Wednesday, 21 January 2015

Filled Under:

Redirect With JavaScript/jQuery

To Redirect a page with Javascript or jQuery different methods or styles could be used.
window.location.href="http://www.someUrl.com";   // similar behavior as clicking on a link

window.location = http://www.someUrl.com;

window.location.replace("http://www.someUrl.com");   // similar behavior as an HTTP redirect

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

document.location.href = 'http://www.someUrl.com';

self.location="http://www.someUrl.com";

top.location="http://www.someUrl.com";

window.navigate("http://www.someUrl.com");   // old-IE-only

$(location).attr('href',"http://www.someUrl.com");   //jQuery

Possible Solutions:

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

A Cross Browser Fix:

This cross-browser fix is a simple function. With this function you don't have to worry about HTTP_REFERER getting lost.
function Redirect (url) {
var ua = navigator.userAgent.toLowerCase(),
isIE = ua.indexOf('msie') !== -1,
version = parseInt(ua.substr(4, 2), 10);

// Internet Explorer 8 and lower
if (isIE && version < 9) {
var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();
}

// All other browsers
else { window.location.href = url; }
}

Redirect('someUrl.html'); //function call with URL argument

0 comments:

Post a Comment