views:

2885

answers:

5

I tried using

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

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

Any suggestions please?

+1  A: 
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

TStamper
+1  A: 

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();">
...
Georg
The jQuery document ready event is not the same as the onload event. The ready event is fired when the DOM is parsed, the onload event is fired when all the items on the page (images etc.) have been loaded.
Jon Benedicto
Also, the syntax for the <body onload=""> is the following: <body onload="sayHi()">. Adding javascript: will not work.
Jon Benedicto
+4  A: 

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

window.onload = function() {
  alert('hi!');
}
tj111
A: 
       <script language="javascript">

    window.onload = onPageLoad();

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