7

I tried using

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

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

Any suggestions please?

Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
Lilz
  • 4,013
  • 13
  • 61
  • 95

4 Answers4

14

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

window.onload = function() {
  alert('hi!');
}
TJ L
  • 23,914
  • 7
  • 59
  • 77
6
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
  • 30,098
  • 10
  • 66
  • 73
4

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 Schölly
  • 124,188
  • 49
  • 220
  • 267
  • 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 Nov 11 '09 at 17:38
  • Also, the syntax for the is the following: . Adding javascript: will not work. – Jon Benedicto Nov 11 '09 at 17:38
2
       <script language="javascript">

    window.onload = onPageLoad();

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