Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert this onclick() function to onload() [duplicate]

Possible Duplicate:
How to make onclick automatically through onload function

So I have the following function which is being called when I click a button. Here is the code:

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

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

like image 340
Aviral Avatar asked Jan 22 '13 06:01

Aviral


2 Answers

There are multiple options for onload event.

In HTML

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

In Javascript

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

In JQuery

$(document).ready(function(){
      myFunction('parameter');
});
like image 69
viki Avatar answered Sep 28 '22 05:09

viki


Put this anywhere on your page, preferably in the <head>

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

See https://stackoverflow.com/a/7911809/584192 for more ways of doing it via jQuery.

like image 39
Samuel Liew Avatar answered Sep 28 '22 05:09

Samuel Liew