如何在不创建页面重新加载的情况下更改Firefox window.location.hash?
我将一些状态信息存储在URL片段中(哈希,无论您叫什么).当我在Chrome和Safari中更改 window.location.hash
时,该页面不会重新加载-这是我想要的行为.当我在Firefox中更改 window.location.hash
时,我会重新加载页面.我该如何预防?
I'm storing some state information in the URL fragment (hash, whatever you call it). When I change the window.location.hash
in Chrome, and Safari the page doesn't reload - this is the behavior I want. When I change window.location.hash
in Firefox, I get a page reload. How do I prevent this?
注意:我在URL中存储状态的原因是为了使userA可以将URL发送给userB,而userB则可以看到同一页面(由AJAX加载).
解决方案:就其价值而言,如果您完全删除包含#"字符的哈希,则将在Firefox(仅?)中重新加载页面.我最后只是确保不要删除整个哈希.
Resolution: For what it's worth, in Firefox (only?) the page will reload if you completely remove the hash including the "#" character. I ended up just making sure not to delete the entire hash.
在您提到的帖子上,这是设置 hash
的代码:
On the post you mentioned, here's the code that sets the hash
:
function (hsh) {
hsh="#"+hsh;
this.current.hash=hsh;
document.location.hash=hsh;
}
这是读取 hash
(例如从另一个URL)的代码:
function () {
if(document.location.hash.split("#").pop()==this.current.hash.split("#").pop()) { return; }//this hash was set by code!
var bl=-1;//-1 = none; otherwise, it's a block index.
var cp=-1;
if(document.location.hash.indexOf(Un.HASH_BLOCK_PREFIX)>-1) {
var blkhsh=document.location.hash.substring(Un.HASH_BLOCK_PREFIX.length+1);
var blknum=parseInt(blkhsh,16);
if(!isNaN(blknum)) {
for(bi in Un.BLOCKS.blFrom) {
if(Un.BLOCKS.blFrom[bi]==blkhsh) {
bl=bi; break;
}
}
}
else {
var blkspc=blkhsh.split(Un.HASH_BLOCK_SPACE).join(" ");
for(bi in Un.BLOCKS.blName) {
if(Un.BLOCKS.blName[bi]==blkspc) {
bl=bi; break;
}
}
}
}
else if(document.location.hash!="") {
var hexhsh=document.location.hash.split("#").pop().split(Un.HASH_CP_PREFIX).pop().split("x").pop(); //if the cp_prefix is missing, or if prefixed with x, this will still work :)
if(hexhsh.length==4 && !isNaN(parseInt(hexhsh,16))) {
cp=hexhsh;
for(bi in Un.BLOCKS.blFrom) {
if(Un.BLOCKS.blFrom[bi]>cp) {
bl=bi-1;
break;
}
}
}
}
if(bl>-1) {
Un.selectBlock(bl,(cp==-1 ? 0 : 1));
}
else {
Un.next(1);
Un.setMenuButton($("#BlockButton"),0);
}
if(cp!=-1) {
setTimeout("Un.cpOpen('"+cp+"')",100);
}
}