在 ajax 调用上设置 window.location.hash
我相信这是一个非常简单的问题。
我正在使用 jquery 进行 ajax 调用,我想要的就是在类似于此的调用之后设置自定义哈希:
window.location.hash = '?url=http://www.sitename.com';
但它在此之前返回 # 符号,我不想要它,
www.mysitename.com/#?url=http://www.sitename.com
所以基本上如何删除该 # 符号并附加一个干净的哈希没有它?
谢谢。
i belive it is quite simple question.
I am making an ajax call with jquery and all that i want is to set custom hash after the call similar to this:
window.location.hash = '?url=http://www.sitename.com';
but it returns # symbol before that and i dont want it
www.mysitename.com/#?url=http://www.sitename.com
so basically how to remove that # symbol and attach a clean hash without it?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。如果您想设置一个查询字符串(
?something=something
),您必须通过更改location.search
来设置它(这样做会导致页面重新加载) (仅查询字符串)或location.href
- 无 AJAXish/Web2.0ish ;)哈希值是
#
符号之后的客户端部分,并且从未发送到服务器。它纯粹是为了定位页面元素(例如是由哈希
#something
定位的),现在将状态信息保存在URL 以便后退/前进按钮在 AJAX 应用程序中继续工作(尽管最终会被 HTML5 的 PushState 函数取代)。如果您仍想使用哈希值,请在 与 Google 兼容的方式。基本上,这意味着您应该在哈希中使用
#!something
,其中something
也可以是经典(非 AJAX)请求中真实 URL 的一部分。You cannot. If you want to set a query string (the
?something=something
stuff) you have to set it (and by doing so cause a page reload) by changinglocation.search
(only the query string) orlocation.href
- nothing AJAXish/Web2.0ish ;)The hash is the client-side part after the
#
sign and never sent to the server. It's purely meant to target page elements (for example a<h2 id="something">
is targeted by the hash#something
) and nowadays to keep state information in the URL so the back/forward buttons keep working in AJAX applications (even though that'll eventually be replaced with HTML5's pushState function).If you still want to use the hash, please do so in a google-compatible way. Basically it means you should use
#!something
in the hash wheresomething
could also be part of the real URL in a classical (non-AJAX) request.URL 中的
哈希
是根据MDC 文档< /a>:请注意,
#
字符(我相信在北美称为“井号”)通常称为“哈希”。您想改为设置
window.location.search
。这是:请注意,这会触发重新加载。如果您不希望这样,则需要使用
hash
属性。The
hash
in a URL is, per the MDC docs:Note that the
#
character (which I believe is called the "pound sign" in North America) is generally called the "hash".You want to set
window.location.search
instead. This is:Note that this triggers a reload. If you don't want this, you need to use the
hash
property.