Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a header location in jQuery? [duplicate]

Tags:

Can I use a header location in jQuery for redirection or refresh?

Like in PHP:

header('location:www.google.co.in');
header("Refresh:1,url=home.php");

If not, what is the alternative way?

like image 955
Deepu Sasidharan Avatar asked Jan 04 '14 10:01

Deepu Sasidharan


2 Answers

Headers are interpreted prior to the rendering of the document, and before jQuery is even loaded so these aren't an option. Instead, you can redirect the browser using document.location.

document.location.href = 'www.google.co.in';

For a jQuery approach you can use

$(location).attr('href', 'www.google.co.in');

however I would favor the plain javascript version.

like image 158
Martin Avatar answered Sep 19 '22 21:09

Martin


You can use:

window.location.assign('www.google.co.in');

or

window.location.href = 'www.google.co.in';

or

window.location.replace('www.google.co.in');

The difference is that assign() will just cause a new document to load. While replace() will replace the current document and replace the current history with that URL making it so you can't go back to the previous document loaded.

like image 39
Stefanos Vakirtzis Avatar answered Sep 20 '22 21:09

Stefanos Vakirtzis