简体   繁体   English

页面完全加载时,确保JavaScript正在运行的最佳方法是什么?

[英]what is the best way to make sure javascript is running when page is fully loaded?

I am trying to run my javascript in my asp.net webform page but I am not sure it runs properly because all the elements are not loaded yet. 我正在尝试在asp.net Webform页面中运行我的JavaScript,但是由于所有元素尚未加载,我不确定它是否可以正常运行。 How can I make sure my script is at the very bottom of the page with using jquery? 如何使用jquery确保我的脚本位于页面的最底部? So it can run when the page is loaded? 这样它可以在页面加载时运行吗?

Even though everyone says use $(document).ready it's kind of an anti-pattern. 即使每个人都说使用$(document).ready它还是一种反模式。

What you really want to do is put any scripts you want to load at the end of the body 您真正想做的是将要加载的所有脚本放在正文末尾

<html>
 <head> ... </head>
 <body>
  ...
  <script src="..." ></script>
</html>

As long as your scripts are at the end of all your other HTML content the javascript will only fire when the content above it has loaded. 只要您的脚本位于所有其他HTML内容的末尾,则javascript仅在其上方的内容已加载时才会触发。

Use .ready() : 使用.ready()

$(document).ready(function($) {
  // page is loaded
});

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. 虽然JavaScript提供了呈现页面时执行代码的load事件,但只有在完全接收到所有资产(例如图像)之后,才会触发此事件。 In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. 在大多数情况下,可以在完全构建DOM层次结构后立即运行脚本。 The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. 保证传递给.ready()的处理程序将在DOM准备就绪后执行,因此通常这是附加所有其他事件处理程序并运行其他jQuery代码的最佳位置。 When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts. 使用依赖CSS样式属性值的脚本时,在引用脚本之前,请先引用外部样式表或嵌入样式元素,这一点很重要。

With jQuery, you want to attach to the ready event. 使用jQuery,您想附加到ready事件。 This gets fired when all DOM elements have been loaded: 加载所有DOM元素后,将触发此事件:

jQuery(document).ready(function(){

  // jQuery Code here

});

Shorthand version: 简写版本:

$(function(){
    // ...
});

Using this method, I usually put my jQuery in an external file, or in the <head> tag. 使用这种方法,我通常将jQuery放在外部文件或<head>标记中。

With pure JavaScript, you can use 使用纯JavaScript,您可以使用

window.onload = function() {
    // Page loaded
};

Or you can use jQuery's ready function: 或者,您可以使用jQuery的ready函数:

$( document ).ready( function() {
    // Dom loaded
} );

Note: jQuery's ready function fires when the DOM has loaded (unless the browser does not support a dom-ready method), not when the whole page has loaded (images, scripts, etc). 注意:jQuery的ready函数在加载DOM时触发(除非浏览器不支持dom-ready方法),而不是在整个页面加载时(图像,脚本等)触发。

You should put your code inside the document ready block as suggested in the JQuery documentation. 您应该按照JQuery文档中的建议将代码放入文档就绪块中。 This way it will be loaded when all the scripts will be loaded. 这样,将在加载所有脚本时加载它。

$(document).ready(function(){
//Put your code here
});

What is the best way to make sure javascript is running when page is fully loaded? 确保页面完全加载后运行javascript的最佳方法是什么?

If you mean "fully loaded" literally, ie, all images and other resources downloaded, then you have to use an onload handler, eg: 如果按字面意思是“完全加载”,即下载了所有图像和其他资源,则必须使用onload处理程序,例如:

window.onload = function() {
   // Everything has loaded, so put your code here
};

If you mean "after all of the HTML has been parsed and all elements are accessible from script", at which point images may still be downloading, then you can either put your script at the bottom of the source HTML or use a document.ready handler. 如果您的意思是“在解析完所有HTML并且可以从脚本访问所有元素之后”,此时可能仍在下载图像,那么您可以将脚本放在源HTML的底部或使用文档。处理程序。 Or both. 或两者。 Refer to any of the other answers for details. 有关详细信息,请参考其他任何答案。

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

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