加载脚本时发出警报

时间:2014-08-05 22:44:04

标签: javascript load alert

我可以使用以下代码加载外部脚本:

newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = 'http://code.jquery.com/jquery-latest.js';
document.getElementsByTagName('head')[0].appendChild(newScript);

但我看了这页:http://www.w3schools.com/jsref/event_onload.asp 我尝试了我的新代码:

<script type="text/javascript">
newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.onload = 'loaded();';
newScript.src = 'http://code.jquery.com/jquery-latest.js';
document.getElementsByTagName('head')[0].appendChild(newScript);

function loaded()
{
alert('The script is loaded');
}
</script>

但它不起作用。我希望在加载脚本时收到警报

1 个答案:

答案 0 :(得分:0)

更改行:

newScript.onload = 'loaded();';

要:

newScript.onload = loaded;