Js anti-blocking loading and js blocking Loading

Source: Internet
Author: User

Js anti-blocking loading and js blocking Loading

Js anti-blocking Loading

<script type="text/javascript">function scriptDomElement (u) {  var s = document.createElement('script');  h = document.getElementsByTagName('body')[0];  s.src = u;  s.async = true;  if(h)h.appendChild(s,h.firstChild);}scriptDomElement('http://*************************?t=20140717');</script>



Javascript loading is not blocked.

This cannot be generalized. If there are few or no external link elements on your page, there will be no download blocking. If your page references a large number of external files, such as css and images, you can directly write all the scripts in the script tag of the page (even below the end of the body Tag) instead of external links, it is also possible that the external css file is not downloaded when the script is running. Generally, the method to be executed during page initialization will be written in the onload event.

How can I determine whether JS loading is complete during dynamic loading?

In the normal loading process, js file loading is synchronized. That is to say, during js loading, the browser will block the parsing of the following content. At this time, dynamic loading is particularly important. Because it is asynchronously loaded, it can be automatically downloaded in the background, without interfering with the normal parsing of other content, you can increase the speed of page loading for the first time.
In IE or some IE kernel-based browsers (such as Maxthon), it is determined by the readystatechange method of the script node. In other browsers, it is usually determined by the load event. The following code:
Function dynamicLoad () {var _ doc = document. getElementsByTagName ('head') [0];
Var script = document. createElement ('script ');
Script. setAttribute ('type', 'text/javascript ');
Script. setAttribute ('src', 'jquery-1.4.js ');
Script. onload = script. onreadystatechange = function (){
If (! This. readyState | this. readyState = 'loaded' | this. readyState = 'complete '){
Alert ('done');} script. onload = script. onreadystatechange = null ;}}
This is because if the script node is not added to the DOM tree, it will not respond to the script load event in chrome and firefox. But IE does .. The modified code is as follows:
Function dynamicLoad () {var _ doc = document. getElementsByTagName ('head') [0];
Var script = document. createElement ('script ');
Script. setAttribute ('type', 'text/javascript ');
Script. setAttribute ('src', 'jquery-1.4.js ');
_ Doc. appendChild (script );
Script. onload = script. onreadystatechange = function (){
If (! This. readyState | this. readyState = 'loaded' | this. readyState = 'complete '){
At this time, all browsers can respond. You can try it ~

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.