Monday, 3 February 2014

How can I make a redirect in jQuery/JavaScript?



for that purpose jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.

It is better than using window.location.href =, because replace() does not put the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href. If you want to simulate an HTTP redirect, use location.replace.

For example:
// similar behavior as an HTTP redirect
window.location.replace("http://codephpsimple.blogspot.com/");

// similar behavior as clicking on a link
window.location.href = "http://codephpsimple.blogspot.com/";
 
 
window.location = "http://www.page-2.com";

Note: This is similar to "clicking" a link and will record page change in browser's history.
To replace current page in history, use location.replace

jQuery approach 


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

WARNING: This answer has been provided as a possible solution. Although, obviously, the pure JavaScript approach is the best one, as this requires jQuery.

How can I get query string values in JavaScript?




Share:

No comments:

Post a Comment