js同步阻塞_非阻塞非同步JS

news/2024/7/19 15:50:38 标签: javascript, php, js, java, laravel
<a class=js同步阻塞" width="403px" height="256px" style="outline: none;" />

js同步阻塞

Update Oct 2013: for a more bulletproof version, tested in the wild, IE and all, check Philip's snippet at http://www.lognormal.com/blog/2012/12/12/the-script-loader-pattern/

2013年10月更新:要获得在野外,IE和所有环境中经过测试的更防弹的版本,请访问http://www.lognormal.com/blog/2012/12/12/the-script-loader-pattern检查Philip的代码段/

Asynchronous JS is cool but it still blocks window.onload event (except in IE before 10). That's rarely a problem, because window.onload is increasingly less important, but still...

异步JS很酷,但是它仍然阻止window.onload事件(IE 10之前的版本除外)。 这很少出现问题,因为window.onload重要性越来越小,但仍然...

At my Velocity conference talk today Philip "Log Normal" Tellis asked if there was a way to load async JS without blocking onload. I said I don't know, which in retrospect was duh! because I spoke about Meebo's non-onload-blocking frames (without providing details) earlier in the talk.

在今天的Velocity会议演讲上,Philip“ Log Normal ” Tellis询问是否有一种加载异步JS而不阻止onload 。 我说我不知道​​,回想起来是哪! 因为我在演讲开始时谈到了Meebo的非阻塞框架(未提供详细信息)。

Stage fright I guess.

我猜是怯场。

Minutes later in a moment of clarity I figured Meebo's way should help. Unfortunately all Meebo docs are gone from their site, but we still have their Velocity talk from earlier years (PPT). There are missing pieces there but I was able to reconstruct a snippet that should load a JavaScript asynchronously without blocking onload.

几分钟后,我想了一下Meebo的方法应该会有所帮助。 不幸的是,所有Meebo文档都已从他们的站点中删除,但是我们仍然有他们早些时候的Velocity演讲( PPT )。 那里缺少一些片段,但是我能够重建一个片段,该片段应异步加载JavaScript而不阻止onload。

Here it goes:

它去了:

(function(url){
  var iframe = document.createElement('iframe');
  (iframe.frameElement || iframe).style.cssText = "width: 0; height: 0; border: 0";
  var where = document.getElementsByTagName('script');
  where = where[where.length - 1];
  where.parentNode.insertBefore(iframe, where);
  var doc = iframe.contentWindow.document;
  doc.open().write('<body onload="'+
    'var js = document.createElement(\'script\');'+
    'js.src = \''+ url +'\';'+
    'document.body.appendChild(js);">');
  doc.close();
})('http://www.jspatterns.com/files/meebo/asyncjs1.php');

The demo page is right here. It loads a script (asyncjs1.php) that is intentionally delayed for 5 seconds.

演示页面就在这里。 它加载一个脚本( asyncjs1.php ),该脚本被故意延迟了5秒钟。

特征 (Features)

  • loads a javascript>javascript file asynchronously

    异步加载JavaScript文件
  • doesn't block window.onload nor DOMContentLoaded

    不阻止window.onloadDOMContentLoaded

  • works in Safari, Chrome, Firefox, IE6789 *

    适用于Safari,Chrome,Firefox,IE6789 *
  • works even when the script is hosted on a different domain (third party, CDN, etc), so no x-domain issues.

    即使脚本托管在其他域(第三方,CDN等)上,该脚本也可以工作,因此没有x域问题。
  • no loading indicators, the page looks done and whenever the script arrives, it arrives and does its thing silently in the background. Good boy!

    没有加载指示器,页面看起来完成,并且每当脚本到达时,它就会到达并在后台静默地执行其操作。 好孩子!

* The script works fine in Opera too, but blocks onload. Opera is weird here. Even regular async scripts block DOMContentLoaded which is a shame.

*该脚本在Opera中也能正常工作,但是会阻止onload 。 歌剧在这里很奇怪。 甚至常规的异步脚本也会阻止DOMContentLoaded ,这很可惜。

退税 (Drawback)

The script (asyncjs1.php) runs is in an iframe, so all document and window references point to the iframe, not the host page.

脚本(asyncjs1.php)运行在iframe中,因此所有documentwindow引用均指向iframe,而不是宿主页面。

There's an easy solution for that without changing the whole script. Just wrap it in an immediate function and pass the document object the script expects:

有一个简单的解决方案,无需更改整个脚本。 只需将其包装在一个立即函数中,然后传递脚本期望的document对象:

(function(document){
 
  document.getElementById('r')... // all fine
 
})(parent.document);

它是如何工作的 (How does it work)

  1. create an iframe without setting src to a new URL. This fires onload of the iframe immediately and the whole thing is completely out of the way

    创建iframe而不将src设置为新的网址。 这会立即触发iframe的onload ,整个过程完全无法进行

  2. style the iframe to make it invisible

    设置iframe的样式以使其不可见
  3. get the last script tag so far, which is the snippet itself. This is in order to glue the iframe to the snippet that includes it.

    获取到目前为止的最后一个script标记,即代码片段本身。 这是为了将iframe粘贴到包含它的代码段。

  4. insert the iframe into the page

    将iframe插入页面
  5. get a handle to the document object of the iframe

    获取iframe document对象的句柄

  6. write some HTML into that iframe document

    将一些HTML写入该iframe文档
  7. this HTML includes the desired script

    该HTML包含所需的脚本

Tell your friends about this post on Facebook and Twitter

在Facebook和Twitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/non-onload-blocking-async-js/

js同步阻塞


http://www.niftyadmin.cn/n/1305997.html

相关文章

js webaudio音量_WebAudio:JS中的振荡器

js webaudio音量How about generating some noise in JavaScript? 如何在JavaScript中产生一些噪音&#xff1f; Demo is here: oscillator. 演示在这里&#xff1a;振荡器。 这是如何运作的&#xff1f; (How does this work?) Using HTML Web Audio you can synthesize aud…

yslow_YSlow开发:设置

yslowAs promised, lets setup for YSlow development using the easiest option - the bookmarklet version. The journey of conquering the world with your rules and extensions... starts with the first step. 如所承诺的&#xff0c;让我们使用最简单的选项-小书签版本…

文化甲板_甲板大厅2012

文化甲板Traditions are traditions. Marry Christmas with a new cover, this time its Deck the Halls: 传统是传统。 与圣诞节一起换上新的封面&#xff0c;这次是Deck the Halls &#xff1a; Deck the halls (part one) 甲板大厅(第一部分) (Its only part one because I …

实时 webaudio_WebAudio:实时输入

实时 webaudioLive input, aka getUserMedia: it exists in Chrome Canary for audio too. Great times to be a web developer, right? 实时输入&#xff0c;也称为getUserMedia &#xff1a;Chrome Canary中也存在音频输入。 成为网络开发人员的美好时光&#xff0c;对吗&am…

getusermedia_Opera 12中的getUserMedia

getusermediaOpera 12 wins - the first stable desktop browser to ship getUserMedia(). I believe they had shipped it already in a mobile version of the browser. Opera 12获胜-第一个稳定的桌面浏览器发布getUserMedia() 。 我相信他们已经在浏览器的移动版本中发布了…

css变量_CSS变量

css变量Weeee, CSS variables just landed in WebKit, this is pretty exciting! Weeee&#xff0c;CSS变量仅位于WebKit中&#xff0c;这非常令人兴奋&#xff01; Unfortunately I couldnt see them in action in WebKit nightly (must be something Im missing), but theyre…

俄窃贼当众盗走名画_公开的;当众

俄窃贼当众盗走名画Update: WebPerfSummit 更新&#xff1a; WebPerfSummit Update 2: SydJS, Anaconda LimoMolly Malones, qCon confirmed, fix *some* Portuguese spelling 更新2&#xff1a; SydJS&#xff0c; Anaconda Limo Molly Malones&#xff0c;已确认qCon&a…

Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use

1 错误描述 Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the ot…