Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash params vs url params, when to use which?

Tags:

url

Is there a convention or best practice advice on when to use hash params, url params, vs paths?

For example:

  • hash params: stackoverflow.com/questions#q=13630937&t=hash-params-vs-url-params
  • url params: stackoverflow.com/questions?q=13630937&t=hash-params-vs-url-params
  • url path: stackoverflow.com/questions/13630937/hash-params-vs-url-params

Are there security, seo, usability benefits or disadvantages of each or is an issue of style?

like image 261
wag2639 Avatar asked Mar 06 '13 03:03

wag2639


People also ask

Should you use URL parameters?

Website URL parameters are commonly used for tracking session IDs, product category page filters, powering search queries and more. Parameters can be valuable but do confuse search engines, resulting in page indexing issues and wasted crawl budget.

What is hash parameter in URL?

Hash parameter authentication. You can authenticate individual REST requests by adding a hash parameter to the URL. The hash parameter allows you to prepare REST requests that can be executed by unauthenticated users. Requests that contain the hash parameter ignore the credentials specified in the authentication header ...

Does the order of URL parameters matter?

A properly written application will find a given query parameter in any order and will not be order sensitive.

Does the parameter string on the URL come before the path?

While the query parameters appear on the right side of the '? ' in the URL, path parameters come before the question mark sign. Secondly, the query parameters are used to sort/filter resources.

Why would you use a hash parameter in a JavaScript page?

This is preferred because if you have a single page javascript application and users navigate and load more content via ajax and share the url, without the hash or a push state modification the person receiving the content would get the homepage or starting state. Hash params can be amended easily and read by javascript without reloading the page.

How to use URL/path parameters?

We use URL / Path Parameters to access a resource on the server. For example, a user who can be found by an ID or a tag. Practical examples are platforms on the Internet that have the following URL syntax: domain.com/<username>. Sometimes there is still an @ sign in front of it. We can implement this simply and elegantly with a URL parameter:

What is the difference between url and query parameters?

Query parameters work almost the same way. But in contrast to URL parameters, we do not define what we expect. All parameters that have been received are available in the req.query object. A new parameter is always marked by the question mark. We can also pass multiple params in the URL. It is important that we separate them with an and sign.

Why can't I get the hash/fragment of a URL?

That's because hash/fragment always comes after the query string: the URL you want to achieve is actually invalid. In fact, with your proposed URL the server will never be able to read the query string, because it is considered part of the hash.


1 Answers

Hash params are useful for single page javascript applications, it allows javascript to present the user with a sharable url for state of the application. This is preferred because if you have a single page javascript application and users navigate and load more content via ajax and share the url, without the hash or a push state modification the person receiving the content would get the homepage or starting state. Hash params can be amended easily and read by javascript without reloading the page.

Hash parameters are usually only used on the client side, hash params wont be passed to the server... so they are only useful for parameterization to the client.

/users#!/13 

would load the user index page and then javascript could read the hash

window.location.hash and pass it through some sort of client side router and make an appropriate ajax request and possibly load the user show template and push it to the dom.

Url params and url path are somewhat interchangeable. People usually use url path for describing restful resources such as

/users/[:id] => /users/13 => /users?id=13 /users/:id/posts => /users/13/posts /users/:user_id/posts/:id => /users/13/posts/22 etc...... 

@Walter Tross, made a good point from an SEO point of view. Slugged urls or "URL Params" are more indexable by crawlers and tend to rank higher.

For params that do not fit in a resourceful route we send them as params

/users?sort=user_name&order=asc 
like image 187
j_mcnally Avatar answered Oct 02 '22 17:10

j_mcnally