Relative URL containing just the querystring

Ross McNab picture Ross McNab · Mar 8, 2013 · Viewed 16.5k times · Source

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?page=13

and I want to link to

http://demo.com/bigreport?page=14

Can I use <a href="?page=14">Next</a> as a relative link for this?

I was surprised to find it works in Chrome. I've never seen it documented or mentioned anywhere, so I'm keen to know if anyone uses this, and if there is wider browser support.

Answer

Ross McNab picture Ross McNab · Mar 8, 2013

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