Potential problems setting window.location.hash

試著忘記壹切 提交于 2019-11-28 22:58:58

window.location.hash has been around since JavaScript was introduced in Netscape Navigator 2 back in 1995. It was first supported by Microsoft in Internet Explorer 3 in 1996. I think you can be reasonably certain that every JS-capable browser supports it.

From a quick glance through the source, it looks as if ReallySimpleHistory makes pretty extensive use of this property, so you may well break it. You might want to use its add(newLocation) method instead (which works by setting window.location.hash).

Thinker

Get:

 var hash = location.hash.slice(1);

Set:

 location.hash = '#' + 'string';

Old thread i know, but window.location.hash is subject to a size limit as well. If you're passing large amounts of data, and want to save state in the hash, you could run into some issues.

IE will give you the error: SCRIPT5 - Access denied. if you try to assign a hash that is over the limit which is super useful.

Just food for thought.

kangax

All "modern" (a.k.a A-Graded) browsers allow to set hash and do not reload the page when doing so. The ones that do reload the page are some of the older ones, such as Safari 2.0.4 and Opera 8.5x.

See my Usenet post on comp.lang.javascript where I explain it in a bit more detail.

Also note, that HTML5 finally specifies that hash setter should change actual hash but not reload the page.

Setting window.location.hash works fine in IE6 & IE7.

In some occasions, reading window.location.hash under IE6 right after a set will return the old value, but the browser has set the hash successfully. An example:

alert(window.location.hash);
window.location.hash = '#newHash';

/* Sometimes, it will return the old value,
   I haven't figured out why it does that, and
   it's rather rare. */
alert(window.location.hash);

If you are just using it to set it, you shouldn't run into any problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!