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

javascript - Execute JS on page load without using jQuery - Stack Overflow

programmeradmin2浏览0评论

So I know that if you use jQuery you can use $(document).load(function(){}); so that any code you write into the function gets executed after the whole page has loaded, but is there a way of doing something similar if you don't use jQuery and just use JS?

For example...

<html>
    <head>
        <script type="text/javascript">
            var box = document.getElementById('box');
            alert(box);
        </script>
    </head>
    <body>
        <div id="box" style="width:200px; height:200px; background-color:#999; 
margin:20px;"></div>
    </body>
</html>  

If I use this method the alert just says null. So is there a way of making the js code run once the page has loaded?

So I know that if you use jQuery you can use $(document).load(function(){}); so that any code you write into the function gets executed after the whole page has loaded, but is there a way of doing something similar if you don't use jQuery and just use JS?

For example...

<html>
    <head>
        <script type="text/javascript">
            var box = document.getElementById('box');
            alert(box);
        </script>
    </head>
    <body>
        <div id="box" style="width:200px; height:200px; background-color:#999; 
margin:20px;"></div>
    </body>
</html>  

If I use this method the alert just says null. So is there a way of making the js code run once the page has loaded?

Share Improve this question edited May 25, 2013 at 20:14 Jerry 71.5k14 gold badges104 silver badges146 bronze badges asked May 25, 2013 at 20:05 user1563944user1563944 4037 silver badges18 bronze badges 2
  • Actually, when using jQuery, please do not use $(document).load(function(){...}); – adeneo Commented May 25, 2013 at 20:09
  • This question might have an answer that you need http://stackoverflow.com/questions/799981... – Tafadzwa Gonera Commented May 25, 2013 at 20:14
Add a comment  | 

4 Answers 4

Reset to default 15

I use:

<script type="text/javascript">
    window.onload = function(){
        //do stuff here
    };
</script>

This way you don't have to use any onload tags in your html.

The easiest way is to simply put your script at the end of the document, typically just before the closing body tag:

<html>
<head>

</head>
<body>
<div id="box" style="width:200px; height:200px; background-color:#999; margin:20px;"></div>
<script type="text/javascript">
    var box = document.getElementById('box');
    alert(box);
</script>
</body>

</html>

You can use a variety of methods to accomplish this.

The simplest, easiest method would be to simply add the script tag at the end of your body tag:

<html>
<head>
    <title> Example </title>
</head>
<body>
    <div>
        <p>Hello</p>
    </div>
    <script type="text/javascript">
        // Do stuff here
    </script>
</body>
</html>

The way jQuery does it is something similar to:

window.onload = function() {
    // Do stuff here
}

I usually just do it the second way, just in case.

To ensure cross-browser compatibility, crawl through the source of jQuery and find what they use.

You can use onload in your body tag.

<head>
   <script type="text/javascript">
      function doSomething() {
         //your code here
      }
   </script>
</head>
<body onload="doSomething()">
发布评论

评论列表(0)

  1. 暂无评论