2011-10-11 36 views
0

我在我的網站上有這個內容框,它有不同的標籤,當你點擊其中一個標籤時,它使用JS來隱藏/顯示內容中的相應div框。問題w/window.location.hash

我也有這個在我的頭給每個DIV內容框裏面它自己的網址:

<script type="text/javascript"> 
    function loadSmugInfo() { 
    if(window.location.hash == '#smuginfo') 
     document.getElementById('contentInfo').style.display = "block"; 
     document.getElementById('contentSlideshow').style.display = "none" 
    } 
</script> 

<script type="text/javascript"> 
    function loadFind() { 
    if(window.location.hash == '#find') 
     document.getElementById('contentMap').style.display = "block"; 
     document.getElementById('contentSlideshow').style.display = "none" 
    } 
</script> 

<script type="text/javascript"> 
    function loadSmugmug() { 
    if(window.location.hash == '#smugmug') 
     document.getElementById('contentSmugInfo').style.display = "block"; 
     document.getElementById('contentSlideshow').style.display = "none" 
    } 
</script> 

<script type="text/javascript"> 
    function loadMain() { 
    if(window.location.hash == '#') 
     document.getElementById('contentSlideshow').style.display = "block" 
    } 
</script> 

<script type="text/javascript"> 
    function loadSlideshow() { 
    if(window.location.hash == '#slideshow') 
     document.getElementById('contentSlideshow').style.display = "block" 
    } 
</script> 

我也有它所以當你點擊一個標籤,它改變了哈希值。

這是我的問題。 當頁面像普通(沒有散列)一樣加載時,第一個和最上面的div仍然不顯示(即使它設置爲在CSS中顯示:block)。

我正在加載上面看到的函數,在內容框上方的圖像上使用onLoad。

任何幫助將不勝感激,我有點緊張的時間表。 讓我知道你是否需要更多信息。 謝謝!

+3

旁邊的問題:你爲什麼要用se爲每個功能分配'

1

這裏有幾個問題。

首先,你不顯示任何方式調用這些函數。您可能想要啓動間隔計時器來輪詢這些函數並定期檢查它們。這將確保他們在頁面加載時進行評估,並且每次散列更改。

您還在評估散列==「#」是否爲默認值,但不會在初始頁面加載時顯示。也許把一個AND調查,以檢查它是否==「」以及。

這是我用於散列輪詢的一些代碼示例。也許它可以幫助。

var start_hash = window.location.hash; 
var recent_hash = ''; 

process_hash(start_hash);  // Start the initial hash check 
setInterval(poll_hash, 100); // Initialize our hash polling interval 

function poll_hash() { 
if (window.location.hash==recent_hash) { return; } // No change in URL hash 
recent_hash = window.location.hash;    // Set to check next time. 
process_hash(recent_hash);       // Changed hash, lets process 
} 

function process_hash(current_hash) { 
// Check for defaults, then set to #home. 
if(!current_hash || current_hash == '' || current_hash == '#') 
    { current_hash='#home'; } 

// Strip the # for the hash 
var hash_id = current_hash.match(/[^#]+/g);     

if(hash_id == 'home') { do something; } 
} 
-2

試試這個:

<script> 

    document.domain = 'facebook.com'; 
    try { 
     try { 
     if (window.opener && window.opener.graphexplorer) {  
      window.opener.graphexplorer.authCallback(window.location.hash); 
     } 
     } catch(e) { } 
    } catch (e) { } 
    window.location.hash = ''; 
    window.close(); 

</script>