页面加载后如何使JavaScript执行?

编程入门 行业动态 更新时间:2024-10-10 21:29:37
本文介绍了页面加载后如何使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执行?

本文发布于:2023-11-27 21:45:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   页面   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!