Relative URL containing just the querystring

前端 未结 2 1578
失恋的感觉
失恋的感觉 2020-12-30 18:49

I have some links in a page which only need to change the querystring portion of the current URL.

E.g. the current page is:

http://demo.com/bigreport?p

相关标签:
2条回答
  • 2020-12-30 19:18

    <a href="?page14">Next</a> works because browsers interpret that as a relative URL. Similar to how linking images on your site might work <img src="logo.gif"/> Relative urls work this way (link is relative to the current page), you don't need to use the full absolute URL.

    Browsers have been supporting this for long long time. People might not be aware of it because browser automatically handles it.

    0 讨论(0)
  • 2020-12-30 19:21

    Further research reveals that <a href="?page=14">Next</a> is a valid relative URL.

    It's documented as part of WHATWG's URL spec http://url.spec.whatwg.org/#relative-state

    The new URL will inherit the base URL's scheme, host, port and path.

    Tested to work on:

    • Chrome
    • IE 7
    0 讨论(0)
提交回复
热议问题