Javascript Jquery - 如果页面加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5346200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery - if page loaded
提问by njaknjak
Is there any trick how to start a function in javascript, which starts when the page is completely loaded?
有什么技巧可以在javascript中启动一个函数,当页面完全加载时启动吗?
回答by Guffa
If you mean when the HTML document has loaded, use the ready
event:
如果您的意思是 HTML 文档加载完毕,请使用该ready
事件:
$(document).ready(function(){
...
});
Or the shorthand:
或者简写:
$(function(){
...
});
If you mean when the page including all style sheets, scripts, images and whatnot has completed loading, use the load
event:
如果您的意思是当包含所有样式表、脚本、图像等的页面已完成加载时,请使用该load
事件:
$(window).load(function(){
...
});
回答by JAAulde
$( window ).bind( 'load', function()
{
//your code in here
} );
回答by gok
Check this: http://api.jquery.com/ready/
检查这个:http: //api.jquery.com/ready/
jQuery(document).ready(function($) {
// Code using $ as usual goes here.
});
回答by Mark Hildreth
This depends on your definition of "load". Check out these two functions in the jQuery docs:
这取决于您对“负载”的定义。在 jQuery 文档中查看这两个函数:
Specifically, you can see the differences in the ready()
function's doc page.
具体来说,您可以在ready()
函数的文档页面中看到差异。
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. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. 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. 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.
In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.
虽然 JavaScript 提供了用于在呈现页面时执行代码的 load 事件,但在完全接收到所有资产(例如图像)之前不会触发此事件。在大多数情况下,一旦完全构建了 DOM 层次结构,就可以运行脚本。传递给 .ready() 的处理程序保证在 DOM 准备好后执行,因此这通常是附加所有其他事件处理程序和运行其他 jQuery 代码的最佳位置。使用依赖于 CSS 样式属性值的脚本时,在引用脚本之前引用外部样式表或嵌入样式元素非常重要。
如果代码依赖于加载的资产(例如,如果需要图像的尺寸),则应将代码放置在加载事件的处理程序中。
回答by Bharat Chodvadiya
When DOM is ready then use .ready event.
当 DOM 准备好后,使用 .ready 事件。
jQuery(document).ready(function(){
//content
});
you also use .load event when page load images.
您还可以在页面加载图像时使用 .load 事件。
jQuery(window).load(function(){
//content
});
回答by Eugene Lai
If you really want your Javascript to run once 'everything' all your HTML is loaded, you can with
如果您真的希望您的 Javascript 在加载所有 HTML 的“所有内容”后运行,您可以使用
window.onload = function(){
// Do stuff
}
回答by Brent Worden
Try binding a load event callback to the last image on the page.
尝试将加载事件回调绑定到页面上的最后一张图像。
回答by sjith
To call a method once the document is loaded completely use
要在文档完全加载后调用方法,请使用
$(document).ready(function(){
...
});
Or else if page is loading through an ajax call use
否则,如果页面是通过 ajax 调用加载的
$('#containerdiv').load('samplepage.jsp', function() {
alert("ajax load complet..");
});
回答by James Westgate
You are looking for the window load event.
您正在寻找窗口加载事件。
In jQuery you attach the load event as follows:
在 jQuery 中,您可以按如下方式附加 load 事件:
$(document).ready(function() {
$(window).load(function() {
}
}
Note that you need to place this inside a document ready for some browsers, IE8 OTTOMH.
请注意,您需要将其放置在为某些浏览器 IE8 OTTOMH 准备好的文档中。