哈希参数与 url 参数,何时使用哪个? [英] Hash params vs url params, when to use which?

查看:24
本文介绍了哈希参数与 url 参数,何时使用哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有关于何时使用哈希参数、网址参数和路径的约定或最佳实践建议?

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

例如:

  • 哈希参数:stackoverflow.com/questions#q=13630937&t=hash-params-vs-url-params
  • url 参数:stackoverflow.com/questions?q=13630937&t=hash-params-vs-url-params
  • 网址路径: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?

推荐答案

Hash params 对于单页 javascript 应用程序很有用,它允许 javascript 向用户呈现应用程序状态的可共享 url.这是首选,因为如果您有一个单页 javascript 应用程序,并且用户通过 ajax 导航和加载更多内容并共享 url,那么在没有哈希或推送状态修改的情况下,接收内容的人将获得主页或开始状态.Hash params 可以轻松修改,无需重新加载页面即可通过 javascript 读取.

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

将加载用户索引页面,然后 javascript 可以读取哈希

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

window.location.hash 并将其通过某种客户端路由器并发出适当的 ajax 请求,并可能加载用户显示模板并将其推送到 dom.

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 路径来描述宁静的资源,例如

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,从 SEO 的角度提出了一个很好的观点.Slugged urls 或URL Params"更容易被抓取工具索引,并且往往排名更高.

@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

这篇关于哈希参数与 url 参数,何时使用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆