Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should come first in URL, hash or querystring?

Some online articles says, that there is no standard for querystring and hash in URL, but we are following what has continued to happen. So, my question is what should be a better way of having both querystring and hash in same URL.

The problem I think is, if hash follows querystring, it can become a value to some querystring data and if querystring follows hash, the whole querystring may become a hask. So, what order should I follow?

like image 829
PranshuKhandal Avatar asked Mar 25 '19 08:03

PranshuKhandal


2 Answers

Some online articles says, that there is no standard for querystring and hash in URL

Either they are wrong or you are misinterpreting them.

The query string must appear before the fragment identifier (which you call the hash).

The specification shows the format of a URI:

URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

It clearly shows the fragment appearing after the query.

if hash follows querystring, it can become a value to some querystring data

It can't. The # is a special character that indicates the start of the fragment. To include one in query string data it needs to be escaped as %23.

like image 115
Quentin Avatar answered Sep 20 '22 22:09

Quentin


This is something I tried in JavaScript:

window.location="alpha#abc?def=34";
console.log(window.location);

The result was:

Location {
  hash: "#abc?def=34",
  search: "",
  ...otherData
}

And, then:

window.location="alpha?abc=34#def";
console.log(window.location);

The result was:

Location {
  hash: "#def",
  search: "?abc=34",
  ...otherData
}

Clearly, JavaScript does not distinguish anything after # symbol, while querystring before hash works fine.

So, we should use querystring first and then hash.

like image 33
PranshuKhandal Avatar answered Sep 19 '22 22:09

PranshuKhandal