How to use [removed].replace javascript?

后端 未结 1 373
被撕碎了的回忆
被撕碎了的回忆 2021-02-01 05:28

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


        
相关标签:
1条回答
  • 2021-02-01 06:05

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

    0 讨论(0)
提交回复
热议问题