简体   繁体   中英

jQuery’s $(document).ready() is it adviceable to use

请我需要一些建议,是否建议使用jQuery的$(document).ready()因为我在使用加载功能加载外部页面时遇到了问题,在此页面上用Google搜索和迷迷糊糊确实想知道我是否可以在不运行jquery脚本的情况下运行$(document).ready()需要您的意见。

Since jQuery manipulates the document, using .ready makes sure your code runs when the document is ready, ie the browser has downloaded the HTML

The load will trigger later, when images are loaded, etc.

Also, place your <script> tag just before the <body> tags ends, this will speed up page loading and contribute to a better user experience.

I think the best way to look at this is that no one thing is a fix-all for coding. The ready call is great but not for everything your JS does. Also, as others have suggested moving your JS before the body tag can help but not always.

Your question should not be should you use it but rather when to use it.

$document.ready() is not called in every partial postback. $document.ready() is called only one time ie during first time of page loading. if you want to get call back of every partial postback then you have to use pageLoad() method which is the part of scriptmanager generated script means if you want to use pageLoad() method then you need to use asp.net scriptmanager in the page.

JQuery is highly recommended for it cross-browser compatibility, community support, ease of use, and battle-tested.

$(document.ready()) is equivalent to:

document.addEventListener("DOMContentLoaded", function(event) {
    // code goes here

});

The window.load function or JQuery load is equivalent to

document.addEventListener("load", function(event) {
    // code goes here

});

The DOMContentLoaded fires when all of the HTML DOM has parsed. Load fires when all of the HTML DOM has parsed and images have finished loading

To use this method you must ensure you have included the JQuery library and it has loaded beforehand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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