javascript document.body 为空或不是对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5417103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:08:00  来源:igfitidea点击:

document.body is null or not an object

javascript

提问by san

I am getting Java script error while loading page: document.body is null or not an object. URL: https://admit-uat.belgacom.be/WCE/ESW/selectBundle/productId/bun_nettv_go

加载页面时出现 Java 脚本错误:document.body 为空或不是对象。网址:https: //admit-uat.belgacom.be/WCE/ESW/selectBundle/productId/bun_nettv_go

Can you please let me know what is the issue.

你能告诉我是什么问题吗?

回答by Hyman Dsilva

<head>
    <script type="text/javascript" charset="utf-8">
        // wait for the DOCUMENT to become ready.
        window.onload=function(){
          walkmydog()
        }
    </script>
</head>

Here's a detailed explanation for overcoming this sort of problem: http://www.javascriptkit.com/dhtmltutors/domready.shtml

这是克服此类问题的详细说明:http: //www.javascriptkit.com/dhtmltutors/domready.shtml

回答by ErickBest

May be late but helpful...

可能会迟到但很有帮助...

You have to make sure you are calling the document.body after the <body>tag is loaded not before it.

您必须确保在<body>加载标签之后而不是在它之前调用 document.body 。

This WILL NOT work:

这将不起作用:

<html>
     <head>
         <script>
              document.body.onload = function(){
                  alert('document Loaded');
               }
</script>

      </head>

      <body>

    </body>
</html>

This WILL work

这将工作

<head>    

    </head>

    <body >        

<script>
    document.body.onload = function(){
    alert('document Loaded');
    }
    </script>

    </body>

    </html>

This also WILL work

这也将工作

</head>

<body onload = "function foo(){
alert('document Loaded');

   } foo();">


</body>

</html>

However, if you insist of having the Javascriptbefore the <body>tag, You could go for jQuery's

但是,如果您坚持Javascript<body>标签之前使用,则可以选择jQuery's

$(function(){

//.....Your code here.
})

This is also one way to guard against cross-browser issues.

这也是防止跨浏览器问题的一种方法。

Hop that helps!

跳有帮助!