在你提到的帖子,下面是設置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);
}
}
使用' preventDefault'? – Teneff 2011-06-10 22:05:18
我會建議找到另一種方式來存儲/緩存狀態信息,而不是像'_cookie_或_javascript' var'_中的'window.location'方法。 – pixelbobby 2011-06-10 22:05:35
您可能需要提供更多信息和測試頁面。我從未在Firefox中看到過這種行爲。例如,使用FF去http://unicodinator.com/並點擊任何字符 - 你應該看到哈希變化,但沒有頁面重新加載(至少,這是我測試過的所有機器上發生的事情,使用FF 4和FF 3.6)。這使用document.location.hash來更改散列。 – jlarson 2011-06-10 22:06:34