在 ajax 调用上设置 window.location.hash

发布于 2024-10-15 07:50:35 字数 314 浏览 9 评论 0原文

我相信这是一个非常简单的问题。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ˇ宁静的妩媚 2024-10-22 07:50:35

你不能。如果您想设置一个查询字符串(?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 changing location.search (only the query string) or location.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 where something could also be part of the real URL in a classical (non-AJAX) request.

小梨窩很甜 2024-10-22 07:50:35

URL 中的哈希是根据MDC 文档< /a>:

URL 中 # 符号后面的部分,包括 # 符号。

请注意,# 字符(我相信在北美称为“井号”)通常称为“哈希”。

您想改为设置window.location.search。这是:

URL 中 ? 后面的部分符号,包括?符号。

请注意,这会触发重新加载。如果您不希望这样,则需要使用hash 属性。

The hash in a URL is, per the MDC docs:

the part of the URL that follows the # symbol, including the # symbol.

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:

the part of the URL that follows the ? symbol, including the ? symbol.

Note that this triggers a reload. If you don't want this, you need to use the hash property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文