How can I redirect the user from one page to another using jQuery or pure JavaScript?

Programise
Jan 14, 2023

--

There are a few different ways to redirect a user from one page to another using jQuery or pure JavaScript. Here are a few examples:

JavaScript and Jquery
  1. Using jQuery:
$(location).attr('href', 'http://www.example.com');

2. Using pure JavaScript:

window.location.href = 'http://www.example.com';

3. Using pure JavaScript with a delay:

setTimeout(function(){
window.location.href = 'http://www.example.com';
}, 3000);

4. Using pure JavaScript to redirect with a button:

<button onclick="location.href='http://www.example.com'">Redirect</button>

This will redirect the user to “http://www.example.com" when the button is clicked.

It’s worth noting that using location.replace() instead of location.href will prevent the current page from going into the browser's history, so the user will not be able to use the back button to return to the previous page.

--

--

No responses yet