在页面加载后执行的JavaScript [英] JavaScript that executes after page load
问题描述
我在< 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屋!