在页面加载后执行的JavaScript [英] JavaScript that executes after page load

查看:83
本文介绍了在页面加载后执行的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在< head> 中使用< script> 执行外部脚本。

I'm executing an external script, using a <script> inside <head>.

现在,脚本在之前执行页面已加载,因此我无法访问< body> 等等。我想在文档加载(HTML完全下载和内存)后执行一些JavaScript。是否有任何事件可以在我的脚本执行时挂钩,并在页面加载时触发?

Now since the script executes before the page has loaded, I can't access the <body>, among other things. I'd like to execute some JavaScript after the document has been "loaded" (HTML fully downloaded and in-RAM). Are there any events that I can hook onto when my script executes, that will get triggered on page load?

推荐答案

这些解决方案将work:

These solutions will work:

<body onload="script();">

document.onload = function ...

甚至是

or even

window.onload = function ...

请注意,最后一个选项是更好的方法,因为它是 unobstrusive 和是认为更标准

Note that the last option is a better way to go since it is unobstrusive and is considered more standard.

这篇关于在页面加载后执行的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆