Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
deleted 45 characters in body
Source Link
Divyesh Kanzariya
  • 3.8k
  • 3
  • 48
  • 47

jQuery code to redirect a page or URL

First Way

Here is the jQuery code for redirecting a page. Since, I have put this code on the $(document).ready() function, it will execute as soon as the page is loaded.

var url = "http://stackoverflow.com";
$(location).attr('href',url);

You can even pass a URL directly to the attr() method, instead of using a variable.

Second Way

 window.location.href="http://stackoverflow.com";

You can also code like this (both are same internally):

window.location="http://stackoverflow.com";

If you are curious about the difference between window.location and window.location.href, then you can see that the latter one is setting href property explicitly, while the former one does it implicitly. Since window.location returns an object, which by default sets its .href property.

Third Way

There is another way to redirect a page using JavaScript, the replace() method of window.location object. You can pass a new URL to the replace() method, and it will simulate an HTTP redirect. By the way, remember that window.location.replace() method doesn't put the originating page in the session history, which may affect behavior of the back button. Sometime, it's what you want, so use it carefully.

// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");

Fourth Way

The location.assign() method loads a new document in the browser window.

window.location.assign("http://stackoverflow.com"); 

The difference between assign() and replace() method is that the location.replace() method deletes the current URL from the document history, so it is unable to navigate back to the original document. You can't use the browsers Back button in this case. If you want to avoid this situation, you should use location.assign() method, because it loads a new Document in the browser.

Fifth Way

like attr() method (after jQuery 1.6 introduce)

var url = "http://stackoverflow.com";
$(location).prop('href', url);

jQuery code to redirect a page or URL

First Way

Here is the jQuery code for redirecting a page. Since, I have put this code on the $(document).ready() function, it will execute as soon as the page is loaded.

var url = "http://stackoverflow.com";
$(location).attr('href',url);

You can even pass a URL directly to the attr() method, instead of using a variable.

Second Way

 window.location.href="http://stackoverflow.com";

You can also code like this (both are same internally):

window.location="http://stackoverflow.com";

If you are curious about the difference between window.location and window.location.href, then you can see that the latter one is setting href property explicitly, while the former one does it implicitly. Since window.location returns an object, which by default sets its .href property.

Third Way

There is another way to redirect a page using JavaScript, the replace() method of window.location object. You can pass a new URL to the replace() method, and it will simulate an HTTP redirect. By the way, remember that window.location.replace() method doesn't put the originating page in the session history, which may affect behavior of the back button. Sometime, it's what you want, so use it carefully.

// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");

Fourth Way

The location.assign() method loads a new document in the browser window.

window.location.assign("http://stackoverflow.com"); 

The difference between assign() and replace() method is that the location.replace() method deletes the current URL from the document history, so it is unable to navigate back to the original document. You can't use the browsers Back button in this case. If you want to avoid this situation, you should use location.assign() method, because it loads a new Document in the browser.

Fifth Way

like attr() method (after jQuery 1.6 introduce)

var url = "http://stackoverflow.com";
$(location).prop('href', url);

First Way

Here is the jQuery code for redirecting a page. Since, I have put this code on the $(document).ready() function, it will execute as soon as the page is loaded.

var url = "http://stackoverflow.com";
$(location).attr('href',url);

You can even pass a URL directly to the attr() method, instead of using a variable.

Second Way

 window.location.href="http://stackoverflow.com";

You can also code like this (both are same internally):

window.location="http://stackoverflow.com";

If you are curious about the difference between window.location and window.location.href, then you can see that the latter one is setting href property explicitly, while the former one does it implicitly. Since window.location returns an object, which by default sets its .href property.

Third Way

There is another way to redirect a page using JavaScript, the replace() method of window.location object. You can pass a new URL to the replace() method, and it will simulate an HTTP redirect. By the way, remember that window.location.replace() method doesn't put the originating page in the session history, which may affect behavior of the back button. Sometime, it's what you want, so use it carefully.

// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");

Fourth Way

The location.assign() method loads a new document in the browser window.

window.location.assign("http://stackoverflow.com"); 

The difference between assign() and replace() method is that the location.replace() method deletes the current URL from the document history, so it is unable to navigate back to the original document. You can't use the browsers Back button in this case. If you want to avoid this situation, you should use location.assign() method, because it loads a new Document in the browser.

Fifth Way

like attr() method (after jQuery 1.6 introduce)

var url = "http://stackoverflow.com";
$(location).prop('href', url);

adding fifth way
Source Link
Divyesh Kanzariya
  • 3.8k
  • 3
  • 48
  • 47

jQuery code to redirect a page or URL

First Way

Here is the jQuery code for redirecting a page. Since, I have put this code on the $(document).ready() function, it will execute as soon as the page is loaded.

var url = "http://stackoverflow.com";
$(location).attr('href',url);

You can even pass a URL directly to the attr() method, instead of using a variable.

Second Way

 window.location.href="http://stackoverflow.com";

You can also code like this (both are same internally):

window.location="http://stackoverflow.com";

If you are curious about the difference between window.location and window.location.href, then you can see that the latter one is setting href property explicitly, while the former one does it implicitly. Since window.location returns an object, which by default sets its .href property.

Third Way

There is another way to redirect a page using JavaScript, the replace() method of window.location object. You can pass a new URL to the replace() method, and it will simulate an HTTP redirect. By the way, remember that window.location.replace() method doesn't put the originating page in the session history, which may affect behavior of the back button. Sometime, it's what you want, so use it carefully.

// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");

Fourth Way

The location.assign() method loads a new document in the browser window.

window.location.assign("http://stackoverflow.com"); 

The difference between assign() and replace() method is that the location.replace() method deletes the current URL from the document history, so it is unable to navigate back to the original document. You can't use the browsers Back button in this case. If you want to avoid this situation, you should use location.assign() method, because it loads a new Document in the browser.

Fifth Way

like attr() method (after jQuery 1.6 introduce)

var url = "http://stackoverflow.com";
$(location).prop('href', url);

jQuery code to redirect a page or URL

First Way

Here is the jQuery code for redirecting a page. Since, I have put this code on the $(document).ready() function, it will execute as soon as the page is loaded.

var url = "http://stackoverflow.com";
$(location).attr('href',url);

You can even pass a URL directly to the attr() method, instead of using a variable.

Second Way

 window.location.href="http://stackoverflow.com";

You can also code like this (both are same internally):

window.location="http://stackoverflow.com";

If you are curious about the difference between window.location and window.location.href, then you can see that the latter one is setting href property explicitly, while the former one does it implicitly. Since window.location returns an object, which by default sets its .href property.

Third Way

There is another way to redirect a page using JavaScript, the replace() method of window.location object. You can pass a new URL to the replace() method, and it will simulate an HTTP redirect. By the way, remember that window.location.replace() method doesn't put the originating page in the session history, which may affect behavior of the back button. Sometime, it's what you want, so use it carefully.

// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");

Fourth Way

like attr() method (after jQuery 1.6 introduce)

var url = "http://stackoverflow.com";
$(location).prop('href', url);

jQuery code to redirect a page or URL

First Way

Here is the jQuery code for redirecting a page. Since, I have put this code on the $(document).ready() function, it will execute as soon as the page is loaded.

var url = "http://stackoverflow.com";
$(location).attr('href',url);

You can even pass a URL directly to the attr() method, instead of using a variable.

Second Way

 window.location.href="http://stackoverflow.com";

You can also code like this (both are same internally):

window.location="http://stackoverflow.com";

If you are curious about the difference between window.location and window.location.href, then you can see that the latter one is setting href property explicitly, while the former one does it implicitly. Since window.location returns an object, which by default sets its .href property.

Third Way

There is another way to redirect a page using JavaScript, the replace() method of window.location object. You can pass a new URL to the replace() method, and it will simulate an HTTP redirect. By the way, remember that window.location.replace() method doesn't put the originating page in the session history, which may affect behavior of the back button. Sometime, it's what you want, so use it carefully.

// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");

Fourth Way

The location.assign() method loads a new document in the browser window.

window.location.assign("http://stackoverflow.com"); 

The difference between assign() and replace() method is that the location.replace() method deletes the current URL from the document history, so it is unable to navigate back to the original document. You can't use the browsers Back button in this case. If you want to avoid this situation, you should use location.assign() method, because it loads a new Document in the browser.

Fifth Way

like attr() method (after jQuery 1.6 introduce)

var url = "http://stackoverflow.com";
$(location).prop('href', url);

Commonmark migration
Source Link

##jQuery code to redirect a page or URL##

jQuery code to redirect a page or URL

First Way

Here is the jQuery code for redirecting a page. Since, I have put this code on the $(document).ready() function, it will execute as soon as the page is loaded.

var url = "http://stackoverflow.com";
$(location).attr('href',url);

You can even pass a URL directly to the attr() method, instead of using a variable.

Second Way

 window.location.href="http://stackoverflow.com";

You can also code like this (both are same internally):

window.location="http://stackoverflow.com";

If you are curious about the difference between window.location and window.location.href, then you can see that the latter one is setting href property explicitly, while the former one does it implicitly. Since window.location returns an object, which by default sets its .href property.

Third Way

There is another way to redirect a page using JavaScript, the replace() method of window.location object. You can pass a new URL to the replace() method, and it will simulate an HTTP redirect. By the way, remember that window.location.replace() method doesn't put the originating page in the session history, which may affect behavior of the back button. Sometime, it's what you want, so use it carefully.

// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");

Fourth Way

like attr() method (after jQuery 1.6 introduce)

var url = "http://stackoverflow.com";
$(location).prop('href', url);

##jQuery code to redirect a page or URL##

First Way

Here is the jQuery code for redirecting a page. Since, I have put this code on the $(document).ready() function, it will execute as soon as the page is loaded.

var url = "http://stackoverflow.com";
$(location).attr('href',url);

You can even pass a URL directly to the attr() method, instead of using a variable.

Second Way

 window.location.href="http://stackoverflow.com";

You can also code like this (both are same internally):

window.location="http://stackoverflow.com";

If you are curious about the difference between window.location and window.location.href, then you can see that the latter one is setting href property explicitly, while the former one does it implicitly. Since window.location returns an object, which by default sets its .href property.

Third Way

There is another way to redirect a page using JavaScript, the replace() method of window.location object. You can pass a new URL to the replace() method, and it will simulate an HTTP redirect. By the way, remember that window.location.replace() method doesn't put the originating page in the session history, which may affect behavior of the back button. Sometime, it's what you want, so use it carefully.

// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");

Fourth Way

like attr() method (after jQuery 1.6 introduce)

var url = "http://stackoverflow.com";
$(location).prop('href', url);

jQuery code to redirect a page or URL

First Way

Here is the jQuery code for redirecting a page. Since, I have put this code on the $(document).ready() function, it will execute as soon as the page is loaded.

var url = "http://stackoverflow.com";
$(location).attr('href',url);

You can even pass a URL directly to the attr() method, instead of using a variable.

Second Way

 window.location.href="http://stackoverflow.com";

You can also code like this (both are same internally):

window.location="http://stackoverflow.com";

If you are curious about the difference between window.location and window.location.href, then you can see that the latter one is setting href property explicitly, while the former one does it implicitly. Since window.location returns an object, which by default sets its .href property.

Third Way

There is another way to redirect a page using JavaScript, the replace() method of window.location object. You can pass a new URL to the replace() method, and it will simulate an HTTP redirect. By the way, remember that window.location.replace() method doesn't put the originating page in the session history, which may affect behavior of the back button. Sometime, it's what you want, so use it carefully.

// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");

Fourth Way

like attr() method (after jQuery 1.6 introduce)

var url = "http://stackoverflow.com";
$(location).prop('href', url);

added 11 characters in body
Source Link
Divyesh Kanzariya
  • 3.8k
  • 3
  • 48
  • 47
Loading
add fourth way
Source Link
Divyesh Kanzariya
  • 3.8k
  • 3
  • 48
  • 47
Loading
add fourth way
Source Link
Divyesh Kanzariya
  • 3.8k
  • 3
  • 48
  • 47
Loading
Copy edited. (its = possessive, it's = "it is" or "it has". See for example <http://www.wikihow.com/Use-Its-and-It%27s>.)
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134
Loading
Source Link
Divyesh Kanzariya
  • 3.8k
  • 3
  • 48
  • 47
Loading