最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getElementById and onLoad - Stack Overflow

programmeradmin0浏览0评论

HTML file ...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ".dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="src/myJS.js"></script>
</head>
<body onload="myJS.myFunction();">
<p id="p01"></p>
</body>
</html>

External Javascript file (called myJS.js for convenience) ...

myJS = {
    myFunction: function()
    {
        //This works
        document.write("Hello world. ");

        //This does not work
        document.getElementById("p01").appendChild(document.createTextNode("Hello world, again"));
    }
};

My best guess is that the node p01 has not been created when myJS is executed, but I thought that onload would do the right thing with it.

HTML file ...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="src/myJS.js"></script>
</head>
<body onload="myJS.myFunction();">
<p id="p01"></p>
</body>
</html>

External Javascript file (called myJS.js for convenience) ...

myJS = {
    myFunction: function()
    {
        //This works
        document.write("Hello world. ");

        //This does not work
        document.getElementById("p01").appendChild(document.createTextNode("Hello world, again"));
    }
};

My best guess is that the node p01 has not been created when myJS is executed, but I thought that onload would do the right thing with it.

Share Improve this question asked Jan 4, 2013 at 12:44 ExtermiknitExtermiknit 1531 gold badge2 silver badges12 bronze badges 4
  • Are you running document.getElementById() after document.write()? – Álvaro González Commented Jan 4, 2013 at 12:48
  • 1 body.onload is triggered as soon as the page code (that exact html/php/asp file) is rendered, not when all the elements have been loaded. window.onload, on the other hand, is triggered when all the elements have been loaded such as scripts, images etc. – inhan Commented Jan 4, 2013 at 12:49
  • What is the problem you are facing ? You should be able to render p01. Are you not getting it ? – Ashwin Commented Jan 4, 2013 at 12:51
  • @inhan I don't think so, body onload=... and window.onload are exactly the same. – bfavaretto Commented Jan 4, 2013 at 12:54
Add a ment  | 

2 Answers 2

Reset to default 3

If your external js really contains both lines of code you posted, the problem is that document.write is overwriting the whole HTML (it behaves like that as soon as the DOM is loaded). Then getElementById won't find any #p01, because it won't exist anymore.

If you simply remove the document.write call, your code is supposed to work (see a live example).

Because p01 is not yet added to the DOM.

Try like this:

<body>
<p id="p01"></p>
<script type="text/javascript">
window.onload = myJS.myFunction;
</script>
</body>
发布评论

评论列表(0)

  1. 暂无评论