Does JSONP make an asynchronous call?
I am new to jsonp and I understand that JSONP is a technique which creates a dynamic script src="..." tag, that wraps the returned javascript(or json object) with a callback function.
But if I am not mistaken, src attribute in a script tag will hold back all further executions until the script loads, so how can it be asynchrounus call?
This related question should shed some light on your question.
Script nodes added dynamically using javascript are executed asynchronously and they won't block execution of other scripts.
Actually, as you can read up on here and here dynamically created <script src="..">
elements after the DOM has finished loaded will NOT be blocking and by that they will be asynchrounus.. at least in the order they are created.
qutoted from http://calendar.perfplanet.com/2010/the-truth-about-non-blocking-javascript/
When inserting a script dynamically, the non-blocking download begins immediately. The script executes as soon as it is downloaded completely. In most browsers, the order of execution is not guaranteed, though Firefox < 4 and Opera will execute the scripts in the order in which they were inserted. This general approach is supported in all major browsers.
I think your question has two parts.
First, JSONP is essentially not about dynamic script tags, rather dynamic script tags are a technique used hand in hand with JSONP.
JSONP is a method which allows site to load content from different domains than the ORIGIN, exploiting the browser's tolerance towards SCRIPT tags with src pointing to external domains. (You should know this by going through the links given in other answers).
Dynamic SCRIPT tags on the other hand provides an Asynchronous nature to any script be it JSONP or otherwise.
Point is, whenever a browser hits a SCRIPT tag on a document, it stops most other activities (rendering DOM specially) until that script is download. This affects users experience on how responsive the site is. Effect of this is even worse if the script is not directly contributing to the primary content of the site (such as Google Ads, Tweets, or Facebook Timeline (Asuming you are not Mark Z. :P), etc)
To avoid this problem, you can inject dynamic SCRIPT tags to the page once it has fully loaded on the browser (ie ready/loaded event). Then the browser will silently load the new script, but user has the full page (almost) rendered for him giving impression of quick loading. In that sense dynamic scripts can be asynchronous to page loading.
However, in practice most of scripts used in this way are JSONP scripts residing on different domains, although it's not a requirement.
Hope this makes sense.
For TRUE async script loading you should look into HTML5 sync attribute:
链接地址: http://www.djcxy.com/p/47500.html上一篇: 我如何使用外部JSON ...?
下一篇: JSONP是否进行异步调用?