window.location在ie8中未定义

时间:2011-10-17 16:57:48

标签: javascript jquery html5

有人知道为什么window.location在ie8中未定义(也许是ie7),但是作为document.location工作?

我无法找到为什么来自其他项目的其他文件有类似的代码,但IE没有问题。

我也得到一个奇怪的错误:'window.location.hash为null或者不是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 );
}

1 个答案:

答案 0 :(得分:6)

项目中的某些内容可能会覆盖window.location,但这在IE 9中不起作用:

var location;

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

您可以使用delete运算符删除变量(虽然从技术上讲,您不应该这样做,但确实有效):

delete location;

但最好的解决方案是在文件中查找有问题的代码。

相关问题