Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use onPageLoad in Javascript?

I tried using

onPageLoad: function() {
    alert("hi");
}

but it won't work. I need it for a Firefox extension.

Any suggestions please?

like image 367
Lilz Avatar asked May 20 '09 18:05

Lilz


3 Answers

If you want to do this in vanilla javascript, just use the window.onload event handler.

window.onload = function() {
  alert('hi!');
}
like image 129
TJ L Avatar answered Sep 19 '22 21:09

TJ L


var itsloading = window.onload;

or

<body onload="doSomething();"></body> 
//this calls your javascript function doSomething

for your example

<script language="javascript">

function sayhi() 
{
  alert("hi")
}
</script>

<body onload="sayhi();"></body> 

EDIT -

For the extension in firefox On page load example

like image 30
TStamper Avatar answered Sep 23 '22 21:09

TStamper


Assuming you meant the onload-event:

You should use a javascript library like jQuery to make it work in all browsers.

<script type="text/javascript">
    $(document).ready(function() {
        alert("Hi!");
    });
</script>

If you really don't want to use a javascript library (Don't expect it to work well in all browsers.):

<script type="text/javascript">
    function sayHi() {
        alert("Hi!");
    }
</script>
<body onload="javascript:sayHi();">
...
like image 40
Georg Schölly Avatar answered Sep 23 '22 21:09

Georg Schölly