2011-10-17 82 views
2

任何人都知道爲什麼window.location在ie8(也許ie7也是)中未定義,但作爲document.location?window.location在ie8中是未定義的

我找不到爲什麼另一個文件,從其他項目有一個類似的代碼,但沒有與IE瀏覽器的問題。

我也得到一個奇怪的錯誤:'window.location.hash爲空或不是jquery 1.6.4第2行中的對象'。我也試過1.5.1,但同樣的錯誤。

標題:

<html lang=""> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
    <link rel="stylesheet" href="assets/css/style.css"> 
    <script src="assets/js/jquery.1.6.4.min.js"></script> 
    <!--[if lt IE 9]> 
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> 
    <![endif]--> 

    <script src="assets/js/jquery.easing.1.3.js"></script> 
    <script src="assets/js/jquery.ba-hashchange.min.js"></script> 
    <script src="assets/js/script.js"></script> 
</head> 

JS部分:

if(window.onhashchange) 
{ 
    window.onhashchange = function() 
    { 
     hashChanged(window.location.hash); 
    } 
} 
else 
{ 
    var storedHash = window.location.hash; 
    window.setInterval(function() 
    { 
     if(window.location.hash != storedHash) 
     { 
      storedHash = window.location.hash; 
      hashChanged(storedHash); 
     } 
    }, 100); 
} 
+3

你是否缺少'<!DOCTYPE html>'或者你沒有粘貼? – canon

+0

它只是*在IE中的問題?或其他瀏覽器呢?你有沒有名爲'location'的變量? – user113716

+0

只是IE。是這樣的,@Ӫ_._Ӫ –

回答

6

的東西在你的項目可能會改寫window.location,儘管這不會在IE 9的工作:

var location; 

alert(window.location); 
//-> "undefined" 

您可以使用delete運算符來刪除變量(儘管technicall你不應該能夠,但它確實工作):

delete location; 

但最好的解決方案是尋找您的文件中的違規的一段代碼。

+1

我剛剛在IE8中證實了這一點。這是一些可怕的東西。 –

+0

我做了一些更糟糕的'window = $(document.window);'。現在,我看到了一塌糊塗。爲什麼這個錯誤代碼在其他瀏覽器上工作?謝謝! –

+0

這不是一個錯誤。這是設計,其他瀏覽器也是這樣做的。如果你在全局範圍內定義一個'var location',那麼它是一個全局變量,因此等同於'window.location'。看起來很嚇人,而且有潛在的危險,但瞭解JavaScript如何工作表明它在技術上是正確的行爲。我覺得有點愚蠢,但有什麼更好的學習方式? :) –