JQuery Ready function Abuse Analysis _jquery

Source: Internet
Author: User
Look at the code written before, there are many such code:
Copy Code code as follows:

$ (function () {
Do something.
});

There may be some initialization styles written here, additional event handling for other DOM elements, and so on. I didn't find anything wrong at first. However, when the page references some slow downloading pictures, the problem arises: after the page HTML is downloaded, the events bound by the binding DOM elements cannot be executed, and the style invalid with the script binding is not valid, which is the ready implementation.

Ok, my situation is very special, maybe this situation for most students is impossible to encounter, but in the development we have to consider in some of the worse circumstances, a user opened your site, and then just on your page there are many pictures ... Then he'll have to wait.

Solution: To be able to innocence
This may be the way we ready before the advent of the show:

Insert a script block into a document
Copy Code code as follows:

<span id= "Test1" >hello world</span>
<script type= "Text/javascript" >
Alert (document.getElementById ("Test1"). InnerHTML);
</script>

Such code executes when the browser is loaded here. Note that if the DOM element here is test1 behind the script block, an exception will appear here.

I just need to execute this code when the page loads, not all the resources on the page are loaded, so there's no need for me to sit here and wait for the "slow" ready.

Well, think about it, sort out the HTML structure.
The page is full of this script block is not good, not conducive to reading, then put them together to draw the end of the document bar.
Copy Code code as follows:

Let's put some title, meta, link, such as God horse things.
<body>
Body html ....
<script type= "Text/javascript" >
Alert ("Pull your code from head to here ...") Ease of operation of the document, do not worry about finding elements, also do not need to window.onload, $.ready god Horse ");
</body>

OK, if you really need to wait for the resource file to be downloaded before you can execute the script, you'll be honest with jquery's ready.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.