7

I tried using

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

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

Any suggestions please?

4

4 回答 4

14

如果你想在 vanilla javascript 中执行此操作,只需使用window.onload事件处理程序。

window.onload = function() {
  alert('hi!');
}
于 2009-05-20T19:08:12.590 回答
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

于 2009-05-20T18:48:04.987 回答
4

假设您的意思是 onload-event:

您应该使用jQuery之类的 javascript 库来使其在所有浏览器中都能正常工作。

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

如果您真的不想使用 javascript 库(不要期望它在所有浏览器中都能正常工作。):

<script type="text/javascript">
    function sayHi() {
        alert("Hi!");
    }
</script>
<body onload="javascript:sayHi();">
...
于 2009-05-20T19:03:46.017 回答
2
       <script language="javascript">

    window.onload = onPageLoad();

    function onPageLoad() {
        alert('page loaded!');
        }
</script>
于 2009-11-11T17:30:21.970 回答