惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

博客园 - RicoRui

【转让】看看有你喜欢的书籍嘛?--都是我翻过的。 TFS --- GrantBackup Plan Permissions Error 用户界面草图设计工具-工具包和资源 IIS URL Rewrite Model & ASP.NET Route Component ‘s Diff Entity Framework 4 & 4.1 [转] LINQ Method cannot be translated into a store expression. Script# C# + Html5 =C3 Project Calculation Rules Proposal for Delayed Script Execution 10个有用的网站访问分析工具 20个免费的seo页面分析工具 网站流量统计定义 Minify JavaScript On the Fly Agile -- Scrum Resource UML with VS2010 Ultimate [转]探究TFS 2010中的测试功能 技术债务 Windows Phone 7 Jump Start
[转]qUIpt:JavaScript Cache Library
RicoRui · 2011-04-15 · via 博客园 - RicoRui

qUIpt 是一个很小很小的 JavaScript Library,所有原始码也才只有 115 行而已(包括批注),我觉得作者 Mario Heiderich 真是太有创意了,他的原理十分简单,使用的 Cache 方法是将数据储存在 window 对象的 name 属性中 ( window.name ),在这里他是将透过 XHR ( XML Http Request ) 取回的 JavaScript 档案内容储存在 window.name 属性里。

通常 window.name 是用在 window.open() 的时候指定的第二的参数,还有当我们在窗体(form)中设定 target 属性的时候,这个 target 指定的就是 window.name 的值,但预设来说 window.name 大部分来讲都是空的,所以 qUIpt 就拿这个属性来做额外的快取(Cache)用途。

qUIpt 基本的运作流程如下:

  • 首先,他先检查你的 window.name 是否有资料。
  • 如果 window.name 是空的,就透过 XHR 动态下载 JavaScript 档案,并将档案内容储存在 window.name 属性中。
  • 如果使用者第一次造访你的网页,或从其他网站进来的,就会先将 window.name 给清空,并重新下载 JS 档回来。
  • 等使用者下次造访你的网页(例如连到下一页),就直接用 eval() 函示将 window.name 的 JS 读出执行,这样所有对象都重新被加载回来了,完全不需要重新下载 JavaScript 檔。

底下这段 code 说明了这个运作流程:

/**
* some security checks to avoid arbitrary off-site
* self.name poisoning
*/if(document.referrer == '' ||
document.referrer.match(
/\w+:\/\/[^\/]+/)[0]
!== location.href.match(/\w+:\/\/[^\/]+/)[0]) {
self.name
= '';
}
/**
* let's start
*/
if(!self.name.length) {
quipt_load();
}
else {
quipt_eval();
}

原理就是这么简单,你可以下载看原始码回来看,才几行而已,也许可以激发你一些更有创意的想法。

由于此项目十分的阳春,毕竟是第一版嘛,经我个人测试之后发现只有 Firefox 能用,IE 目前是无法执行的,所以我小修了一下程序把一些仅支持 Firefox 的语法给修正了,底下的 code 应该可以运作于 Firefox 与 IE 7 ( 其他版本的 IE 我没测试过 )。

/**
* quipt - caching in the name of performance
*
* @author Mario Heiderich <mario.heiderich@gmail.com> , Will 保哥 <doggy.huang@gmail.com>
* @license LGPL
*/
(
function quipt_init() {/**
* the config object
*/
var quipt_config = {/**
* the method to call when loading is done
*
* hint: just set it to false if not needed
*/
'starter' : 'my_init_function', /**
* add files to load to this array
*
* hint: you can add as many files as you wish - as long as they
* come from the same domain. quipt also supports proxied
* off-domain scripts.
*/
'files' : [
'jquery-1.2.6.min.js',
'dummy-js-file-01.js',
'dummy-js-file-02.js'
],
/**
* don't touch this - private stuff! else!
*/
'status' : 0
}
var quipt_code = [];/**
* the loader method - it monitors the status of the ressource
* fetching and kick-starts the XHR method
*
*/
var quipt_load = function() {
quipt_request();
}
/**
* this method just fires XHR requests against the files given in the
* config literal until all ressources are fetched
*
*/
var quipt_request = function() {
try {
console.log(
'loading ressources from filesystem');
}
catch(e) {}var x = new XMLHttpRequest();
x.open(
'GET', quipt_config.files[quipt_config.status], true);
x.send(
null);
x.onreadystatechange
= function() {
if(x.readyState == 4 && x.status == 200) {
quipt_code.push(x.responseText);
quipt_config.status
++;
self.name
+= x.responseText + ';\n';
if(quipt_config.status < quipt_config.files.length) {
quipt_request();
}
else {
quipt_eval();
}
}
}
}
/**
* the eval method - which is called if window.name contains
* the right data
*/
var quipt_eval = function() {
self.eval(self.name);
if(quipt_config.starter) {
self[quipt_config.starter]();
}
}
/**
* some security checks to avoid arbitrary off-site
* self.name poisoning
*/
if(document.referrer == '' || document.referrer.match(/\w+:\/\/[^\/]+/)[0]
!== location.href.match(/\w+:\/\/[^\/]+/)[0]) {
self.name
= '';
}
/**
* let's start
*/
if(!self.name.length) {
quipt_load();
}
else {
quipt_eval();
}
})();