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

javascript - How to call function on page load in JS? - Stack Overflow

programmeradmin3浏览0评论

I have a js function which looks like this

 function showProducts(){
    document.getElementById("shopList").innerHTML = "<ul><li>Test Text</li></ul>";
 }

It's a function that has to show an array of my products. I've made an div with id="shopList" in my html page

        <div id="shopList">
        </div>

But how can I call the function to show me the text in the div? It works when I use this as my body tag but I'm not allowed to write any js code in my html or to use onload or onclick. I'm trying to do it with listeners for almost 4 hours and I still did not find a solution. Could someone help me?

     <body onload="showProducts()">

I have a js function which looks like this

 function showProducts(){
    document.getElementById("shopList").innerHTML = "<ul><li>Test Text</li></ul>";
 }

It's a function that has to show an array of my products. I've made an div with id="shopList" in my html page

        <div id="shopList">
        </div>

But how can I call the function to show me the text in the div? It works when I use this as my body tag but I'm not allowed to write any js code in my html or to use onload or onclick. I'm trying to do it with listeners for almost 4 hours and I still did not find a solution. Could someone help me?

     <body onload="showProducts()">
Share Improve this question edited Dec 12, 2012 at 17:40 Mike 49.5k30 gold badges118 silver badges179 bronze badges asked May 15, 2012 at 1:37 user1395001user1395001 2
  • I'd prefer using jquery.. much easier. – simultsop Commented May 15, 2012 at 1:41
  • If you can't write JS in your html, does that mean your JS is included in an external file? If you put the <script> element that includes your JS (whether in an external file or inline) at the bottom of the body then you can just call showProducts() directly without any event handlers because by the time it runs your div element will have been parsed (and thus can be accessed from JS). Or is your question really "How do I use .addEventListener()?" – nnnnnn Commented May 15, 2012 at 1:51
Add a ment  | 

7 Answers 7

Reset to default 6

Using pure javascript:

window.onload = function(){

};

(or

function doLoad() {
    //Do stuff on load
}

window.onload = doLoad;

With Jquery

$(document).ready(function(){   

}); 

It's not difficult with listeners. Here is a solution (not cross-browser):

document.addEventListener("DOMContentLoaded", showProducts);

Really, assigning to onload is just shorthand for doing it with listeners. This should work , though I haven't tested it.

window.addEventListener("load", showProducts);

With Jquery you could do something like this :

$(document).ready(function(){   
   showProducts();
}); 

It waits untill the page is loaded and then executes the function. You just put it in an external .js file and include it in your page.

(For the people downvoting this answer because it's Jquery, he said he couldn't use onload() so I just mentioned this option. )

Just place the script at the bottom:

<body>
    ...
    <script type="text/javascript">
        myFunction();
    </script>
</body>

John Resig's simplified version from "Pro JavaScript Techniques". It depends on addEvent.

var ready = ( function () {
  function ready( f ) {
  if( ready.done ) return f();

  if( ready.timer ) {
    ready.ready.push(f);
  } else {
    addEvent( window, "load", isDOMReady );
    ready.ready = [ f ];
    ready.timer = setInterval(isDOMReady, 13);
  }
};

function isDOMReady() {
   if( ready.done ) return false;

   if( document && document.getElementsByTagName && document.getElementById && document.body ) {
     clearInterval( ready.timer );
     ready.timer = null;
     for( var i = 0; i < ready.ready.length; i++ ) {
       ready.ready[i]();
     }
     ready.ready = null;
     ready.done = true;
   }
}

  return ready;
})();

window.onload would work, but it is a different beast. jQuery's $(document).ready() is much more plex and better in most scenarios.

Given your criteria of "no script in the HTML" and "no onload or onclick listener", you can put the function into a separate file and run it from a script element at the foot of the page:

<script type="text/javascript" src="showproducts.js"></script>

so now you have no script in the page and no listeners. The code will be executed when the element is added to the DOM, so as long as it is after the related DIV, you're fine.

Incidentally, you don't even need a function, you can just put the statements from the function body into the file.

发布评论

评论列表(0)

  1. 暂无评论