I'm working on javascript/jquery in a php based site, and I have to redirect the page upon an event in the page.
lets say, on clicking the button "click me" in "page1.php", the page should be redirected to "page2.php". This redirection has to be done using javascript/ jquery. Both pages are in the same folder, and the redirection code should use 'RELATIVE LINKS'.
Now, I am able to redirect if I give the absolute link to page2.php, but could someone let me know how to do the same with a relative link?
something like:
window.location.href = 'page2.php'; thanks.
It is quite simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a new page, you just need to add a line in your head section as follows.
“If you need to change the URL of a page as it is shown in search engine results, we recommend that you use a server-side 301 redirect. This is the best way to ensure that users and search engines are directed to the correct page. The 301 status code means that a page has permanently moved to a new location.”
Redirects allow you to forward the visitors of a specific URL to another page of your website. In Site Tools, you can add redirects by going to Domain > Redirects. Choose the desired domain, fill in the URL you want to redirect to another and add the URL of the new page destination. When ready, click Create.
window.location.href = "page2.php"; this worked. Infact, relative and absolute works in the same way:
window.location.href = "http://www.google.com"; // or window.location.href = "page2.html";
You can do a relative redirect:
document.location.href = '../'; //one level up or
document.location.href = '/path'; //relative to domain or in jquery ...
var url = "http://stackoverflow.com"; $(location).attr('href',url);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With