2010-11-06 28 views
3

有沒有什麼方法可以告訴window.location.hash是否定義?Javascript window.location.hash是否定義?

如果已設置,那麼我將獲取變量的值,並使用它在頁面上顯示額外的內容。

+0

的可能重複(HTTP://計算器。 com/questions/298503/how-can-you-check-for-a-hash-in-a-url-using-javascript) – 2013-10-08 12:38:43

回答

12
if(window.location.hash) { 
    // do stuff! 
} 
+2

只要小心if window.location.hash =='#';在某些瀏覽器上它會顯示爲'#',而在其他瀏覽器上顯示爲''。 – zzzzBov 2010-11-07 03:38:54

2

如何:?你怎麼能在URL中使用JavaScript檢查是否有#hash]

if(window.location.hash !== '') 
{ 
} 

或者

if(typeof window.location.hash !== 'undefined') 
{ 
    //your code 
} 
+0

我覺得Hamish可以有更好的答案:)但是如果你的問題看到它是否可用,我可能會證明是有用的。 – 2010-11-06 22:54:23

相關問題