javascript - Internet Explorer 7+ 返回错误 "Object expected",但其他浏览器都不会返回错误

标签 javascript internet-explorer internet-explorer-7

每次我在 Internet Explorer 7 或更高版本中加载页面时,都会收到错误 Object Expected当调用下面的函数时。该脚本在结束之前出现在页面底部 </body>标签。没有具有相同 name 的元素或id .

<script type="text/javascript">
     window.onload = show();
</script>

它尝试调用的 Javascript 函数是

function show() {
    obj1
     = document.getElementById("container").innerHTML
     = '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p>'
         + '<p><a href="#" onclick="hide();">test</a></p></div>';
}
  1. 为什么此错误不会出现在任何其他浏览器中?
  2. 我需要更改哪些内容才能解决该问题?

编辑 0

如果我移动函数show进入同一个区 block window.onload , hide()现在不再有效。

Javascript代码

function show() {
    obj1
     = document.getElementById("container").innerHTML
     = '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p>'
         + '<p><a href="#" onclick="hide();">test</a></p></div>';
}

function hide() {   
    obj1 = document.getElementById("container");
    if(obj1){
        alert("Hi");
        obj1.style.display = "none";
        obj1.style.visibility = "hidden";
    }else{
        alert("Cannot find the element with id container.");
    }
}

HTML 代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict/EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtm1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset="UTF-8" />
    <meta http-equiv="content-language" content="en-us" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="author" content="" />
    <meta name="copyright" content="&copy; 2012" />

    <title>takeover</title>

    <base href="" />
    <link rel="stylesheet" href="" />

    <style type="text/css" media="all" />

    #container {
         position:absolute;
         text-align:center;
         background-color:#fff;
         z-index:9999;
    }

    </style>

    <script type="text/javascript" src="takeover.js"></script>
</head>
<body>
    <div>
        <div id="container"></div>
        <p><a href="#">qwe</a></p>
    </div>

<script type="text/javascript">
     window.onload = show;
</script>
</body>
</html>

编辑 1

使用非 Internet Explorer 浏览器放置 alert(show) 时出现的消息之前window.onload

enter image description here

编辑2

删除所有空格后显示的消息。同样,这仅适用于非 Internet Explorer 浏览器。

enter image description here

编辑3

尝试了 window.show = function show() 和 window.hide = function hide() 但在 Internet Explorer 中仍然出现错误。错误如下所示。

enter image description here

编辑4

这是更新后的代码,所有函数都在一个文件中。这在任何其他浏览器中都不起作用,我收到错误 show未定义。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict/EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtm1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset="UTF-8" />
    <meta http-equiv="content-language" content="en-us" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="author" content="" />
    <meta name="copyright" content="&copy; 2012" />

    <title>takeover</title>

    <base href="" />
    <link rel="stylesheet" href="" />

    <style type="text/css" media="all" />

    #container {
         position:absolute;
         text-align:center;
         background-color:#fff;
         z-index:9999;
    }

    </style>


</head>
<body>
    <div>
        <div id="container"></div>
        <p><a href="#">qwe</a></p>
    </div>

<script type="text/javascript">
    alert(show)
    window.show = function show() {
obj1 = document.getElementById("container").innerHTML =  '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p><p><a href="#" onclick="hide();">test</a></p></div>';
}

window.hide = function hide() { 
    obj1 = document.getElementById("container");
     if(obj1)
 {
   alert("Hi");
   obj1.style.display = "none";
   obj1.style.visibility = "hidden";
 }
 else
 {
   alert("Cannot find the element with id container.");
 }
}
     window.onload = window.show;
</script>
</body>
</html>

最佳答案

这一行...

window.onload = show();

应该是这个...

window.onload = show;

...因为您需要将 show 函数本身分配给 window.onload,而不是调用它的返回值。

关于javascript - Internet Explorer 7+ 返回错误 "Object expected",但其他浏览器都不会返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11007242/

相关文章:

javascript - Highcharts 图表在 IE9 中不显示

visual-studio - Azure VM 中的 Visual Studio 2017 - 无法更改 IE 安全区域

css - 使用兼容模式在 Internet Explorer 8 中进行基本 CSS 调试

javascript - 在页面中加载远程 JavaScript 文件

javascript - Marionette.js 关于事件以及如何正确解除绑定(bind)的问题

javascript - 查询后不包含对象名称?

jquery - $(this).css ("background", "transparent");在 IE 中不工作

html - 我的 <nav> 元素根本没有出现在 IE7 中

javascript - 序列化 : How to query model by 1:n association, 但在单个查询中包含所有关联对象 (PostgreSQL)

javascript - "sencha app watch"到底在做什么?