Internet Explorer 7+返回错误"Object expected",尽管其他浏览器都没有

Pea*_*key 1 javascript internet-explorer internet-explorer-7

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

<script type="text/javascript">
     window.onload = show();
</script>
Run Code Online (Sandbox Code Playgroud)

它试图调用的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>';
}
Run Code Online (Sandbox Code Playgroud)
  1. 为什么此错误不会出现在任何其他浏览器中?
  2. 我需要更改什么才能解决问题?

编辑0

如果我将函数移动show到同一个块中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.");
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

编辑1

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

在此输入图像描述

编辑2

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

在此输入图像描述

编辑3

试过window.show = function show()和window.hide = function hide()但是在Internet Explorer中仍然出错.错误显示如下.

在此输入图像描述

编辑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>
Run Code Online (Sandbox Code Playgroud)

小智 5

这条线......

window.onload = show();
Run Code Online (Sandbox Code Playgroud)

应该是......

window.onload = show;
Run Code Online (Sandbox Code Playgroud)

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