24

My URLs

http://www.mysite.com/folder1/page1.aspx
http://www.mysite.com/folder1/page1.aspx?id=1
http://www.mysite.com/folder1/page1.aspx?id=1&dt=20111128

Redirecting Page

http://www.mysite.com/folder1/page2.aspx

I want to redirect from page1.aspx to page2.aspx

How to write a javascript in page1.aspx?

window.location.replace("/page2.aspx");
window.location.replace("../page2.aspx");
window.location.replace("~/page2.aspx");

First 2 gave me this.

http://www.mysite.com/page2.aspx

Last 1 gave me this.

http://www.mysite.com/folder1/~/page2.aspx

What is the correct way to use?

2
  • What exactly are you trying to do? Commented Nov 28, 2011 at 9:45
  • looks like you are needing a form of url rewriting? Commented Nov 28, 2011 at 9:52

1 Answer 1

60

Include no path information at all, just like in a link:

window.location.replace("page2.aspx");

Here's a live example The example switches between

http://jsbin.com/asupup/2   -- The "2" corresponds to your "page1.aspx"

...and

http://jsbin.com/asupup/3   -- The "3" corresponds to your "page2.aspx"

...and so the 2 page uses

window.location.replace("3");

...and the 3 page uses

window.location.replace("2");

For more about how URLs (and in particular relative URLs) work, see RFC3986. But basically:

  • If a relative URL doesn't start with . or /, it replaces the last segment. So:

        http://foo.com/one/two/page.html
      + bar.html
      = http://foo.com/one/two/bar.html
    
  • If a relative URL starts with ../, it replaces the last segment and the one above it:

        http://foo.com/one/two/page.html
      + ../bar.html
      = http://foo.com/one/bar.html
    

    Note that the two subfolder has been replaced. Multiple ../s can be used to move up multiple levels:

        http://foo.com/one/two/three/four/page.html
      + ../../bar.html
      = http://foo.com/one/two/bar.html
    
  • If a relative URL starts with a single /, it replaces everything after the hostname (and port, if any). So:

        http://foo.com/one/two/page.html
      + /bar.html
      = http://foo.com/bar.html
    
        http://foo.com:8080/one/two/page.html
      + /bar.html
      = http://foo.com:8080/bar.html
    
  • If a relative URL starts with //, it replaces everything following the protocol, so:

        http://ex.com/folder/page.html
      + //foo.com
      = http://foo.com
    

    (This is handy when loading resources and you want to avoid worrying about http vs. https and mixed-content warnings.)

Sign up to request clarification or add additional context in comments.

6 Comments

It doesn't. It will load the same page.
@william: Not if you're not on page2.aspx. If you're on http://www.mysite.com/folder1/page1.aspx (with or without query strings after it), the above will take you to http://www.mysite.com/folder1/page2.aspx. This is the most basic form of relative url.
@william - if you think about it the second and third URL formats from your question are also consistent with what T.J. has advised.
which means.. it will not work if my URL is http://www.mysite.com/folder1 or http://www.mysite.com/folder1/page1.aspx?id=52 ??
@william: When you give a simple page name as shown above, it replaces the last segment of the base URL. So if you're on http://www.mysite.com/folder1/page1.aspx and the relative URL is page2.aspx, you end up with http://www.mysite.com/folder1/page2.aspx. If your starting point is different, you'll end up with a different result (because that's the point of a relative URL). The starting points http://www.mysite.com/folder1 and http://www.mysite.com/folder1/page1.aspx?id=52 are different, the second one is deeper in the hierarchy than the first.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.