预加载脚本而不执行 [英] Preload script without execute

查看:17
本文介绍了预加载脚本而不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了提高页面性能我需要预加载脚本,我需要在底部页面上运行.

In order to improve the page performance I need to preload scripts that I will need to run on the bottom page.

我想控制脚本的解析、编译和执行时间.

I would like to take control of when the script is parsed, compiled and executed.

我必须避免使用脚本标签,因为它是常见渲染引擎的拦截器(极客等).

I must avoid the script tag, because it is blocker for common render engines (geeko, etc).

我无法使用 defer 属性加载它,因为我需要控制脚本的执行时间.此外,async 属性是不可能的.

I can't load it using defer property, because I need to control when the script is executed. Also, async property is not a possibility.

示例:

<html><head>
//preload scripts ie: a.js  without use the script
</head><body> ..... all my nice html here
//execute here a.js
</body></html>

这使我能够最大限度地提高页面的渲染性能,因为浏览器将开始下载脚本内容,并且会同时并行渲染页面.最后,我可以添加脚本标签,这样浏览器就会解析、编译和执行代码.

This allows me to maximize the render performance of my page, because the browser will start to donwload the scripts content, and it will render the page at the same time in parallel. Finally, I can add the script tag, so the browser will parse, compile and execute the code.

我能做到的唯一方法是使用隐藏的图像标签.(这是一个简化的Stoyan 版本)

The only way that I could do that is using a hidden image tag. (This is a simplified version of Stoyan)

 <html><head>
 <img src="a.js" style=display:none;>
</head><body> ..... all my nice html here
 <script src="a.js">  
</body></html>

问题

我在使用这种技术时没有发现任何问题,但有人知道更好的方法吗?是否有任何元预取?

Question

I didn't find any problem using this technique, but does anyone know a better way to do this? Is there any meta prefetch?

我正在使用 requirejs,因此我尝试预加载模块代码,而不执行它,因为此代码取决于 DOM 元素.

I'm using requirejs, so I'm trying to preload the modules code, without executing it, because this code depends of DOM elements.

推荐答案

您应该查看以下链接:

http://calendar.perfplanet.com/2011/lazy-evaluation-of-commonjs-modules/

http://tomdale.net/2012/01/amd-不是答案/

关于 ember.js 如何使用称为 minispade 的工具和 ruby​​ 预处理来加快加载、解析和运行 javascript 模块的过程.

And at how ember.js is using a tool called minispade and preprocessing with ruby to make the process of loading, parsing and running javascript modules fast.

这篇关于预加载脚本而不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆