How do you use onPageLoad in Javascript?

43.9k Views Asked by At

I tried using

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

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

Any suggestions please?

4

There are 4 best solutions below

0
TStamper On
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

2
Georg Schölly On

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();">
...
0
TJ L On

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

window.onload = function() {
  alert('hi!');
}
1
Diamond On
       <script language="javascript">

    window.onload = onPageLoad();

    function onPageLoad() {
        alert('page loaded!');
        }
</script>