开发者

Set window.location.hash on ajax call

i belive it is quite simple question.

I am making an ajax call with jquery and all that i want is to set cu开发者_StackOverflow中文版stom 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜