Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do a relative path redirection using javascript?

Tags:

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.

like image 532
arun nair Avatar asked Nov 06 '11 17:11

arun nair


People also ask

Can we redirect a page using JavaScript?

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.

What is JavaScript 301 redirect?

“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.”

How do I create a redirect URL?

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.


2 Answers

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"; 
like image 69
arun nair Avatar answered Oct 20 '22 00:10

arun nair


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); 
like image 29
Glory Raj Avatar answered Oct 19 '22 23:10

Glory Raj