Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative URL containing just the querystring

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.

like image 462
Ross McNab Avatar asked Mar 08 '13 10:03

Ross McNab


People also ask

What is QueryString in URL?

On the Internet, a querystring (also called an HTTP querystring) is part of the set of characters automatically input in the address bar of a dynamic Web site when a user makes a request for information according to certain criteria.

What is a relative URL example?

Rather than including the entire URL for each page you link, relative URLs cut down on the workload and time needed. For example, coding /about/ is much faster than https://www.example.com/about .

What is relative URL in HTML example?

Relative URL is used to add a link to a page on the website. For example, /contact, /about_team, etc. Everything inside <a>… </a> tag becomes a hyperlink.


2 Answers

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
like image 150
Ross McNab Avatar answered Oct 03 '22 05:10

Ross McNab


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

like image 27
Amy Avatar answered Oct 03 '22 03:10

Amy