我回到了基础,以更好地理解JavaScript。我有一个测试网站。这里是我的代码javascript函数的执行顺序
<html>
<head>
test
</head>
<script type="text/javascript">
function saySomething(message)
{
alert(message);
}
saySomething('Hello world!');
function addLoadListener(fn){
window.addEventListener('load', fn, false);
alert("the previous alert came from onload");
}
addLoadListener(saySomething);
</script>
<body>
<div>blah blah blah</div>
</body>
</html>
所以,有这个功能,saySomething
,提醒Hello World!
。我将它添加到addLoadListener
,以便在页面加载过程中调用它。直到这部分工作良好。但在此之后,当代码运行时会再次调用,并提醒[object Event]
而不是Hello World!
。为什么?我错过了什么?
感谢
尝试小提琴,一切正常http://jsfiddle.net/X3vLP/1/ –
@PratikJoshi谢谢帕特里克,但是我点看到另一个'你好世界'!在页面加载之后。如果您提供解释为什么会发生这种情况,那也是很好的。谢谢 – slevin
@PratikJoshi您必须将jsFiddle的加载机制从'onLoad'更改为'No wrap - in
' – friedi