How to ensure that the execution order of JavaScript jquery.html is not the omnipotent key

Source: Internet
Author: User
Tags subdomain name

1. Introduction
In the previous article "how to ensure the execution sequence of JavaScript-jquery.htmldeep analysis", we revealed that the jquery.html function can be executed dynamically in JavaScript in various browsers. The secret is that AJAX can be synchronized to obtain external JavaScript.
Let's simply download the htmlsource code (test2.htm ):
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> </title>
<Script src = "js/jquery-1.4.4.js" type = "text/javascript"> </script>
<Script>
$ (Function (){
Certificate ('{container'{.html ('<script src = "./service. ashx? File = js/jquery-ui.js & delay = 2000 "type =" text \/javascript "> <\/script> '+' <script> alert (typeof (jQuery. ui); <\/script> ');
});
</Script>
</Head>
<Body>
<Div id = "container">
</Div>
</Body>
</Html>

By the way, external JavaScript loaded in this way cannot be debugged in Firebug, because the external JavaScript parsing after AJAX ends is the same as the inline JavaScript parsing (both call jQuery. globalEval ):


Next, let's go to the topic of this article: Can jquery.html ensure the sequential execution of JS in each browser if the JS of the website is in its own domain?
2. Establish Test Cases
1) Prepare two domain names
To test, I created two subdomains temporarily under my home page (http://sanshi.me/), respectively:
Http://test.sanshi.me/
Http://test1.sanshi.me/
2) Update the demo (test2_1.htm)
I will place test2_1.htm under the first subdomain name. The access address is http://test.sanshi.me/jsorder/test2_1.htm. the source code is as follows:
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> </title>
<Script src = "js/jquery-1.4.4.js" type = "text/javascript"> </script>
<Script>
$ (Function (){
Certificate ('{ininer'{.html ('<script src = "http://test1.sanshi.me/jsorder/service.ashx? File = js/jquery-ui.js & delay = 2000 "type =" text \/javascript "> <\/script> '+' <script> alert (typeof (jQuery. ui); <\/script> ');
});
</Script>
</Head>
<Body>
<Div id = "container">
</Div>
</Body>
</Html>

As you can see, the jQueryUI script points to the second domain name (test1.sanshi. me ).
3) test in different browsers

Test2_1.htm

UseJQuery.htmlFunction dynamically LoadsJavaScript

Firefox 3.6

IE 8

Chrome 10

Safari 4

Opera 11


3. jQuery.html is not a omnipotent key, Then
The secret is not the omnipotent key. This makes us wonder if the internal logic of these two examples is the same?
But the second article is not clear. Do we use the synchronous AJAX mechanism to load external JS? And so on ......
Have you found any problems from the above analysis that AJAX is used to load resources in other domains? Isn't this a clear violation of the well-known single-Origin Policy? Therefore, jQuery cannot be implemented in this way. Let's take a look at the jQuery. ajax document:

It seems that the function (EvalScript) Internal synchronization is not implemented.AJAXTo obtain data:


4. go deep into the jQuery. ajax function to see how to load JS files in different domains

The comment has been clearly written.GETRequest MethodJavaScriptFile, and the file is in another domain (RemoteInHeadAddScriptLabel, insteadAJAX. Therefore, at the end of this condition branch, return directly from the function body:


After analysis, we found thatJavaScript,JQuery.htmlIn factHeadAddScriptTag Method (whether externalJSOr inlineJS), Which is the same as what we mentioned in the first article.Test3.htmIt is the same logic, which also verifies our ideas:

 

It can be seen that if you want to be compatibleCDNTo accelerate static resources, you must also use the"Solution 1: How to dynamically addScriptLabel execution order".

5. Postscript

This is the end of the series. But in my testJQuery1.5.1And I found thatJQuery1.4.4There are differences. I have heard thatJQuery1.5.1PairAJAXPartially restructuredI didn't expect it to really affect our code.

 

For details about the impact, see the next article: how to ensureJavaScriptExecution sequence-OfJQuery1.5.1AndJQuery1.4.4. To be continued...

Related Article

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.