Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

javascript - 页面加载后如何使JavaScript执行?(How to make JavaScript execute after page load?)

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

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

Now since the script executes before the page has loaded, I can't access the <body> , among other things.

(现在,由于脚本是页面加载之前执行的,因此我无法访问<body> 。)

I'd like to execute some JavaScript after the document has been "loaded" (HTML fully downloaded and in-RAM).

(在文档“加载”后(HTML已完全下载并在RAM中),我想执行一些JavaScript。)

Are there any events that I can hook onto when my script executes, that will get triggered on page load?

(执行脚本时,是否有任何我可以钩住的事件会在页面加载时触发?)

  ask by Robinicks translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 .

(请注意, 最后一个选项是更好的选择,因为它不引人注目并且被认为是更标准的 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...