页面加载后如何使JavaScript执行? [英] How to make JavaScript execute after page load?

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

问题描述

我正在使用< head> 内的< script> 执行外部脚本。 / p>

现在,由于脚本在页面加载之前执行,因此我无法访问< body> 等。在文档加载后(HTML已完全下载并在RAM中),我想执行一些JavaScript。在脚本执行时,是否有任何事件可以挂接到页面上?

解决方案

这些解决方案将工作:

 < body onload = script();> 

  document.onload =函数... 

甚至

  window.onload =函数... 

请注意,最后一种选择是更好的选择,因为它是 ,并且考虑了更多标准

>

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

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?

解决方案

These solutions will work:

<body onload="script();">

or

document.onload = function ...

or even

window.onload = function ...

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

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

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