2011-06-10 160 views
3

我在URL片段中存儲了一些狀態信息(散列,無論你怎麼稱呼它)。當我在Chrome中更改window.location.hash並且Safari不重新載入頁面時 - 這是我想要的行爲。當我在Firefox中更改window.location.hash時,我得到一個頁面重新加載。我如何防止這種情況?如何在不創建頁面重新加載的情況下更改Firefox window.location.hash?

注意:我在URL中存儲狀態的原因是,userA可以將url發送到userB,並且userB將能夠看到相同的頁面(由AJAX加載)。


分辨率:對於它的價值,在Firefox(?只)的頁面將重新加載,如果你完全刪除哈希包括「#」字符。我最終確定不要刪除整個散列。

+0

使用' preventDefault'? – Teneff 2011-06-10 22:05:18

+0

我會建議找到另一種方式來存儲/緩存狀態信息,而不是像'_cookie_或_javascript' var'_中的'window.location'方法。 – pixelbobby 2011-06-10 22:05:35

+2

您可能需要提供更多信息和測試頁面。我從未在Firefox中看到過這種行爲。例如,使用FF去http://unicodinator.com/並點擊任何字符 - 你應該看到哈希變化,但沒有頁面重新加載(至少,這是我測試過的所有機器上發生的事情,使用FF 4和FF 3.6)。這使用document.location.hash來更改散列。 – jlarson 2011-06-10 22:06:34

回答

1

有一個名爲history的jQuery插件可以做到這一點。我建議你看看源代碼(上次我檢查時它不是很大)。它也適用於舊版本的IE,但它確實很討厭:P

1

在你提到的帖子,下面是設置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); 
    } 
} 
相關問題