How to convert this onclick () function to onload ()

Possible duplicate:
How to make onclick automatically via onload function

So I have the following function called when a button is clicked. Here is the code:

<a href="#" role="button" onClick="myFunction('parameter')">

      

How can I call this function right after the page is loaded? I tried the onLoad function but doesn't seem to work.

+3


source to share


5 answers


There are several options for the onload event.

In HTML

<body onload="myFunction('parameter')">

      

In Javascript



window.onload=function(){
myFunction('parameter');
};

      

In JQuery

$(document).ready(function(){
      myFunction('parameter');
});

      

+3


source


Place this anywhere on your page, preferably in <head>

<script type="text/javascript">    
$(function() {
    myFunction('parameter');
});
</script>

      



See fooobar.com/questions/1091347 / ... for more options to do this via jQuery.

+3


source


Have you tried this:

window.onload = function(){
    myFunction('parameter');
}

      

See more at: https://developer.mozilla.org/en-US/docs/DOM/window.onload

+2


source


You are calling myFunction document.ready or just before the closing body tag.

Inside document.ready

Live Demo

<script type="text/javascript">    
    $(document).ready(function(){
          myFunction('parameter');
    });
</script>

      

Before closing the body tag

<script type="text/javascript">    

     myFunction('parameter');

</script>

      

+1


source


You can use the following code.

<script>
   onload = function(){
     myFunction('parameter');
   }
</script>

      

+1


source







All Articles