简体   繁体   English

html页面中Javascript onload事件和普通脚本的区别

[英]Difference between Javascript onload event and plain script in the html page

What is the difference between these two pieces of code:这两段代码有什么区别:

Sample 1:示例 1:

<script type="text/javascript">
     function myfunc () {
                alert('hi');
            }
         window.onload = myfunc;
</script>   

Sample 2:示例 2:

<script type="text/javascript">
     alert('hi');//no function used 
</script>

Both pieces of code execute successfully.两段代码都成功执行。

The window.onload makes it so that all DOM elements are loaded before the script runs - that is, if your script modifies or uses DOM elements, then it should be attached to window.onload (or something equivalent in a framework). window.onload使得所有 DOM 元素在脚本运行之前加载 - 也就是说,如果您的脚本修改或使用 DOM 元素,那么它应该附加到window.onload (或框架中的等价物)。 If it is independent of the DOM you can use either.如果它独立于 DOM,您可以使用两者之一。 Refer to the Mozilla Developer Network page for more information.有关更多信息,请参阅Mozilla 开发人员网络页面。 Note that inlined scripts that don't run from window.onload can run once the parser reaches it - it doesn't wait for the rest of the DOM to be loaded.请注意,不从window.onload运行的内联脚本可以在解析器到达它时运行 - 它不会等待 DOM 的其余部分被加载。

第一个在页面完成加载后执行,另一个在解析后立即执行如果您想查看差异,请复制两个代码片段(复制粘贴两次)并查看它们的行为

Sample 1 and Sample 2 do the same thing.样本 1 和样本 2 做同样的事情。 However, window.onload in Sample 1 executes the function when the HTML content (and all images, stylesheets and remote scripts) are fully loaded (not when all the DOM elements are loaded).但是,示例 1 中的 window.onload 会在 HTML 内容(以及所有图像、样式表和远程脚本)完全加载时(而不是在加载所有 DOM 元素时)执行该函数。

Sample 2 does the same as Sample 1 but executes the script instantly.示例 2 的功能与示例 1 相同,但会立即执行脚本。 You will see the difference when you have a page that takes a while to fully load.当您的页面需要一段时间才能完全加载时,您会看到不同之处。 Your test page might have been basic so that it seemed that they execute at the same time (when in fact window.onload executed after sample 2).您的测试页面可能是基本的,因此它们似乎是同时执行的(实际上 window.onload 在示例 2 之后执行)。

window.onload is normally used when you need to execute Javascript code after the page loaded. window.onload 通常用于在页面加载后需要执行 Javascript 代码时。 The inline script in sample two can be used if you want Javascript to execute INSTANTLY after a particular DOM Element has been loaded in the browser for example.例如,如果您希望 Javascript 在浏览器中加载特定 DOM 元素后立即执行,则可以使用示例二中的内联脚本。

onload event triggers when the page has loaded completely including images, audio, etc. But directly writing a statement will execute instantaneously. onload事件在页面完全加载完成后触发,包括图片、音频等。但是直接写一条语句会立即执行。 Hence, onload alert will trigger after the other one.因此,加载警报将在另一个之后触发。

Check this DEMO检查这个演示

Another important difference is that , since the second script executes immediately , You won't be able to access any DOM elements which comes after that script.另一个重要区别是,由于第二个脚本立即执行,您将无法访问该脚本之后的任何 DOM 元素。

For example if you have a DIV element just before the ending of the tag , you cannot use document.getElementById ( or similar DOM accessing function ) to get that particular DIV.例如,如果您在标记结束之前有一个 DIV 元素,则不能使用 document.getElementById(或类似的 DOM 访问函数)来获取该特定 DIV。

But with the first script, which will be executed only after the page load , You can access any element in the DOM但是对于第一个脚本,只有在页面加载后才会执行,你可以访问DOM中的任何元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM