25

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.

5
  • 1
    Why don't just use hyperlinks, setting href with script, if necessary? Commented Nov 6, 2011 at 17:11
  • 4
    Your "something like" suggestion should work. Commented Nov 6, 2011 at 17:12
  • 2
    Oops.. that actually works. The bug due to which redirection was not working in my code was infact due to some other line of code. This line - window.location.href = "whateverpage.php" works, and it is relative path. So I believe redirection is that simple, be it absolute/ relative. Thanks! Commented Nov 6, 2011 at 17:20
  • please flag errostacks or your own answer as correct Commented Nov 6, 2011 at 18:42
  • @kritzikratzi - Yes, I tried flagging the appropriate on as the answer, but then StackOverflow requires me to wait for some 2 days before I can do that. Probably something the site should consider reworking, as the author should have the privilege to mark "answered" whenever he wants. Commented Nov 7, 2011 at 5:10

4 Answers 4

33
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";
Sign up to request clarification or add additional context in comments.

Comments

14

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);

Comments

6

To set a relative path use window.location.pathname.

Take a look at the MDN docs on window.location:

4 Comments

If you are on, say, example.com/index.html, then window.location.pathname will be /index.html
@unclenorton, I'm not sure what you're getting at.
I mean that, if there is filename in path, like index.html, or index.php, or whatever else, it will get into window.location.pathname. For example, go to jigsaw.w3.org/css-validator/documentation.html and type check window.location.pathname in javascript console.
Yes, because that's the path that you're currently located to. For it to contain the filename is valid.
3

If I understand, if you are on http://example.com/path/to/page1.php, and you click on "page2.php", you would like to be redirected to http://example.com/path/to/page2.php ?

Perhaps there is a smarter solution, but does it solve your problem ?

function relativeRedir(redir){
  location.pathname = location.pathname.replace(/(.*)\/[^/]*/, "$1/"+redir);
}

Example of use : relativeRedir("page1.php");

2 Comments

I think this is the only multi-browser solution. document.location.href does work in IE and Chrome but it does not work in FF.
If anyone is still reading this, that's because it should be window.location.href.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.