<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pzling &#187; Javascript</title>
	<atom:link href="http://pzling.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://pzling.com</link>
	<description></description>
	<lastBuildDate>Sun, 08 Apr 2012 02:20:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>给Link标签添加 onload 事件</title>
		<link>http://pzling.com/2012/04/cross-browser-stylesheet-preloading/</link>
		<comments>http://pzling.com/2012/04/cross-browser-stylesheet-preloading/#comments</comments>
		<pubDate>Wed, 04 Apr 2012 14:51:39 +0000</pubDate>
		<dc:creator>pzling</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[link标签onload]]></category>
		<category><![CDATA[跨域CSS加载事件]]></category>

		<guid isPermaLink="false">http://pzling.com/?p=1015</guid>
		<description><![CDATA[下午回来后一直头疼，到现在还在敲代码，喵了个咪的。晚上遇到给link添加onload事件的兼容性问题。查了一些资料还有一些框架代码，发现IE与OPERA比较好处理，主要问题出在Gecko与WebKit内核的浏览器上。 其中一种方法是通过link['sheet']['cssRules']去验证加载情况，但这种方法有跨域报错的问题。本以为我也只能使用这种方法去实现，之后意外翻到了两遍&#8221;鸟语&#8221;资料，跨域下同样可以验证加载情况。简单的试了一下，Safari 5.0、Chromium 18.0 均通过，无论是404、500等状态，均会执行回调函数。 方法就是定时循环document.styleSheets里的ownerNode，看是否有你添加的node。头痛，就不写太多了，具体的自己浏览下面的文章吧 Cross Browser Stylesheet Preloading Faking Onload for Link Elements]]></description>
			<content:encoded><![CDATA[<p>下午回来后一直头疼，到现在还在敲代码，喵了个咪的。晚上遇到给link添加onload事件的兼容性问题。查了一些资料还有一些框架代码，发现IE与OPERA比较好处理，主要问题出在Gecko与WebKit内核的浏览器上。</p>
<p>其中一种方法是通过link['sheet']['cssRules']去验证加载情况，但这种方法有跨域报错的问题。本以为我也只能使用这种方法去实现，之后意外翻到了两遍&#8221;鸟语&#8221;资料，跨域下同样可以验证加载情况。简单的试了一下，Safari 5.0、Chromium 18.0 均通过，无论是404、500等状态，均会执行回调函数。</p>
<p>方法就是定时循环document.styleSheets里的ownerNode，看是否有你添加的node。头痛，就不写太多了，具体的自己浏览下面的文章吧</p>
<ul>
<li>
<a href="http://yearofmoo.com/2011/03/cross-browser-stylesheet-preloading/">Cross Browser Stylesheet Preloading</a>
</li>
<li>
<a href="http://www.zachleat.com/web/load-css-dynamically/">Faking Onload for Link Elements</a>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://pzling.com/2012/04/cross-browser-stylesheet-preloading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>javascript练习题</title>
		<link>http://pzling.com/2012/02/javascript-exercises/</link>
		<comments>http://pzling.com/2012/02/javascript-exercises/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 12:08:03 +0000</pubDate>
		<dc:creator>pzling</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[javascript练习题]]></category>
		<category><![CDATA[js练习题]]></category>

		<guid isPermaLink="false">http://pzling.com/?p=948</guid>
		<description><![CDATA[1、找出数字数组中最大的元素（使用Match.max函数） 2、转化一个数字数组为function数组（每个function都弹出相应的数字） 3、给object数组进行排序（排序条件是每个元素对象的属性个数） 4、利用JavaScript打印出Fibonacci数（不使用全局变量） 5、实现如下语法的功能：var a = (5).plus(3).minus(6); //2 6、实现如下语法的功能：var a = add(2)(3)(4); //9 网上看到这几道题，马上动手试了一下，除了第六题需要点小知识，其它的有点JS基础的都可以写出来，代码质量问题而已，有兴趣的朋友不妨也试试。 第一题，基础题。 第二题，一开始自己实现了个简单的map功能，之后查资料发现map功能已经自带，可以点这里查看关于map函数说明。 第三题，此题主要问题在获取object属性个数上，读完题目后想到的是自己写个函数获取，后来查了一下找到了keys/Object.keys这个函数，可以把object自身的属性键名转为数组，然后直接length获取长度，可以点这里查看关于keys函数说明 第四题，基础题。 第五题，基础题。 第六题，此题完全写不出来只好查资料，看到valueOf、toString后茅塞顿开，立刻明白此题的关键。]]></description>
			<content:encoded><![CDATA[<ol style="margin-bottom:30px;">
<li>1、找出数字数组中最大的元素（使用Match.max函数）</li>
<li>2、转化一个数字数组为function数组（每个function都弹出相应的数字）</li>
<li>3、给object数组进行排序（排序条件是每个元素对象的属性个数）</li>
<li>4、利用JavaScript打印出Fibonacci数（不使用全局变量）</li>
<li>5、实现如下语法的功能：var a = (5).plus(3).minus(6); //2</li>
<li>6、实现如下语法的功能：var a = add(2)(3)(4); //9</li>
</ol>
<p>网上看到这几道题，马上动手试了一下，除了第六题需要点小知识，其它的有点JS基础的都可以写出来，代码质量问题而已，有兴趣的朋友不妨也试试。</p>
<div>第一题，基础题。</div>
<div style="border:1px solid #DDD;">
<iframe style="width: 100%; height: 80px" src="http://jsfiddle.net/pzling/YTzTK/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
</div>
<div>第二题，一开始自己实现了个简单的map功能，之后查资料发现map功能已经自带，<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map">可以点这里查看关于map函数说明</a>。</div>
<div style="border:1px solid #DDD;">
<iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/pzling/HPTKH/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
</div>
<div>第三题，此题主要问题在获取object属性个数上，读完题目后想到的是自己写个函数获取，后来查了一下找到了keys/Object.keys这个函数，可以把object自身的属性键名转为数组，然后直接length获取长度，<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys">可以点这里查看关于keys函数说明</a></div>
<div style="border:1px solid #DDD;">
<iframe style="width: 100%; height: 250px" src="http://jsfiddle.net/pzling/ChbBw/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
</div>
<div>第四题，基础题。</div>
<div style="border:1px solid #DDD;">
<iframe style="width: 100%; height: 120px" src="http://jsfiddle.net/pzling/NsqEr/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
</div>
<div>第五题，基础题。</div>
<div style="border:1px solid #DDD;">
<iframe style="width: 100%; height: 180px" src="http://jsfiddle.net/pzling/ZgXX7/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
</div>
<div>第六题，此题完全写不出来只好查资料，看到valueOf、toString后茅塞顿开，立刻明白此题的关键。</div>
<div style="border:1px solid #DDD;">
<iframe style="width: 100%; height: 250px" src="http://jsfiddle.net/pzling/LXNsg/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
</div>
]]></content:encoded>
			<wfw:commentRss>http://pzling.com/2012/02/javascript-exercises/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jq插件 &#8211; TestingUI [用户调查、测试题插件]</title>
		<link>http://pzling.com/2011/12/jq-plugin-testingui/</link>
		<comments>http://pzling.com/2011/12/jq-plugin-testingui/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 13:19:13 +0000</pubDate>
		<dc:creator>pzling</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[testingui]]></category>
		<category><![CDATA[调查、测试插件]]></category>

		<guid isPermaLink="false">http://pzling.com/?p=836</guid>
		<description><![CDATA[前段时间根据需求做的功能，当时只需要单选就可以。我想把复选功能加上后再整理出来但一直都没什么时间。这星期同事说能不能加上复选功能，我马上就答应了，今天硬抽了时间把复选功能补上了。 简介：该功能适合做用户调查、测试题等类似的应用。界面与功能算分得比较清，所以界面可以自行设计，随便修改，一些元素的样式名与规定的一致就可以了。DEMO里代码也有做一些注释，IE6下会报脚本错误，把JS文件里的中文注释去掉就可以了，如果还有什么不清楚，欢迎给我留言或发邮件 DEMO]]></description>
			<content:encoded><![CDATA[<p>前段时间根据需求做的功能，当时只需要单选就可以。我想把复选功能加上后再整理出来但一直都没什么时间。这星期同事说能不能加上复选功能，我马上就答应了，今天硬抽了时间把复选功能补上了。</p>
<p class="box">简介：该功能适合做用户调查、测试题等类似的应用。界面与功能算分得比较清，所以界面可以自行设计，随便修改，一些元素的样式名与规定的一致就可以了。DEMO里代码也有做一些注释，<strong>IE6下会报脚本错误，把JS文件里的中文注释去掉就可以了</strong>，如果还有什么不清楚，欢迎给我留言或发邮件</p>
<div><a href="http://demo.pzling.com/plugin_testingui/" title="TestingUI [用户调查、测试题插件]"><img src="http://demo.pzling.com/plugin_testingui/view.png" alt="TestingUI [用户调查、测试题插件]" /></a></div>
<div><a href="http://demo.pzling.com/plugin_testingui/" title="TestingUI [用户调查、测试题插件]" class="demo-view">DEMO</a></div>
]]></content:encoded>
			<wfw:commentRss>http://pzling.com/2011/12/jq-plugin-testingui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>不常见的前端BUG收集</title>
		<link>http://pzling.com/2011/11/collection-of-rare-bug/</link>
		<comments>http://pzling.com/2011/11/collection-of-rare-bug/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 14:41:06 +0000</pubDate>
		<dc:creator>pzling</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[DD_belatedPNG背景偏移]]></category>
		<category><![CDATA[IE6绝对定位消失]]></category>
		<category><![CDATA[前端BUG收集]]></category>

		<guid isPermaLink="false">http://pzling.com/?p=819</guid>
		<description><![CDATA[无聊~别人都发常见的，我就在这记录些平时比较少遇到的BUG吧(不定时更新)，以下提到的方法不是唯一解决办法或者一定有效。 IE6绝对定位层消失 当相邻标签有浮动可能出现该问题，对绝对定位的层加清除浮动 IE6下DD_belatedPNG背景位置偏移 有时会发现背景图偏移了1px，你+/-1px它还是老样子，+/-2px它就老实的动了2px，解决办法是修改一下背景图(sprite)的位置1px，好像纵横度坐标都是一样是双数或者单数的原因。 IE7下正负内外边距实现等高的方法，高度没有以最高列为准 可能是没有浮动导致。我遇到的就是一边左浮动，右边不浮动，结果高度总以右边的高度为准，加上浮动后问题解决。]]></description>
			<content:encoded><![CDATA[<p class="box">无聊~别人都发常见的，我就在这记录些平时比较少遇到的BUG吧(不定时更新)，<strong>以下提到的方法不是唯一解决办法或者一定有效</strong>。</p>
<ul style="margin-bottom:50px;">
<li>
<h2>IE6绝对定位层消失</h2>
<p>当相邻标签有浮动可能出现该问题，对绝对定位的层加清除浮动</p>
</li>
<li>
<h2>IE6下DD_belatedPNG背景位置偏移</h2>
<p>有时会发现背景图偏移了1px，你+/-1px它还是老样子，+/-2px它就老实的动了2px，解决办法是修改一下背景图(sprite)的位置1px，好像纵横度坐标都是一样是双数或者单数的原因。</p>
</li>
<li>
<h2>IE7下正负内外边距实现等高的方法，高度没有以最高列为准</h2>
<p>可能是没有浮动导致。我遇到的就是一边左浮动，右边不浮动，结果高度总以右边的高度为准，加上浮动后问题解决。</p>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://pzling.com/2011/11/collection-of-rare-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NodeJS + expressJS + mongoDB环境搭建</title>
		<link>http://pzling.com/2011/10/use-ubuntu-environment-to-build-nodejs-expressjs-mongodb/</link>
		<comments>http://pzling.com/2011/10/use-ubuntu-environment-to-build-nodejs-expressjs-mongodb/#comments</comments>
		<pubDate>Sat, 15 Oct 2011 09:22:10 +0000</pubDate>
		<dc:creator>pzling</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[expressjs]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[nodejs env]]></category>

		<guid isPermaLink="false">http://pzling.com/?p=782</guid>
		<description><![CDATA[呃&#8230;..标题应该是“Ubuntu下NodeJS + expressJS + mongodb环境搭建”，位置不够显示！！！ 接触nodejs后才学了点linux的命令，所以在搭建过程中遇到不少问题，中间补了一些linux的基本常识。环境搭建不断的装删折腾了N多次了，现在算是配得比较熟练了（貌似这没什么用!!）。最近刚装了ubuntu 11.10正式版，环境再次重新搭建了一次，趁现在还安装步骤比较清晰，记录一下搭建过程。 特别注意！！ linux新手，不了解权限问题（例如我），请不要随便用超级用户去执行以下命令，除非你直接用超级用户登录的！！ 有些安装过程可能会出现卡着不动(3-5分钟还是不动的话)，ctrl+c停掉再多执行几次直到成功就可以了 我使用的版本：nodejs 0.4.12，mongodb 2.0，express由npm自动安装 搭建前的准备工作 1：搭建过程需要的依赖包 sudo apt-get install g++ curl libssl-dev 2. 安装git工具（如果你从git上获取源码的话）： sudo apt-get install git-core 获取源码 NodeJS : git://github.com/joyent/node.git ExpressJS: git://github.com/visionmedia/express.git (像我这种不懂手动安装的，就直接用npm安装) mongoDB：我从官网下的，github地址童鞋们自己挖吧！！ NodeJS安装 假设环境我搭建在当前用户work目录下，首先进入解压出来的nodejs目录下，然后执行下面的命令操作(目录自己替换) mkdir ~/work ./configure –-prefix=$HOME/work/node make make install 上面等它执行完毕就算安装成功了，然后设置下环境变量，这样就不用每次开启终端都要进入到特定目录才能执行node命令。(PS:环境变量设置非必要步骤，只是方便之后的使用、操作) echo &#8216;export PATH=$HOME/work/node/bin:$PATH&#8217; >> ~/.profile echo &#8216;export NODE_PATH=$HOME/work/node:$HOME/work/node/lib/node_modules&#8217; >> ~/.profile [...]]]></description>
			<content:encoded><![CDATA[<p>呃&#8230;..标题应该是“Ubuntu下NodeJS + expressJS + mongodb环境搭建”，位置不够显示！！！</p>
<p class="box">接触nodejs后才学了点linux的命令，所以在搭建过程中遇到不少问题，中间补了一些linux的基本常识。环境搭建不断的装删折腾了N多次了，现在算是配得比较熟练了（貌似这没什么用!!）。最近刚装了ubuntu 11.10正式版，环境再次重新搭建了一次，趁现在还安装步骤比较清晰，记录一下搭建过程。</p>
<h2>特别注意！！</h2>
<ul>
<li>linux新手，不了解权限问题（例如我），请不要随便用超级用户去执行以下命令，除非你直接用超级用户登录的！！</li>
<li>有些安装过程可能会出现卡着不动(3-5分钟还是不动的话)，ctrl+c停掉再多执行几次直到成功就可以了</li>
<li>我使用的版本：nodejs 0.4.12，mongodb 2.0，express由npm自动安装</li>
</ul>
<h2>搭建前的准备工作</h2>
<ul>
<li>
<p>1：搭建过程需要的依赖包</p>
<p>sudo apt-get install g++ curl libssl-dev</p>
</li>
<li>
<p>2. 安装git工具（如果你从git上获取源码的话）：</p>
<p>sudo apt-get install git-core</p>
</li>
</ul>
<h2>获取源码</h2>
<ul>
<li>NodeJS : git://github.com/joyent/node.git</li>
<li>ExpressJS: git://github.com/visionmedia/express.git  (像我这种不懂手动安装的，就直接用npm安装)</li>
<li>mongoDB：我从<a href="http://www.mongodb.org/">官网</a>下的，github地址童鞋们自己挖吧！！</li>
</ul>
<div class="more-wrap"><span id="more-782"></span></div>
<h2>NodeJS安装</h2>
<p>假设环境我搭建在当前用户work目录下，首先进入解压出来的nodejs目录下，然后执行下面的命令操作(目录自己替换)</p>
<ul>
<li>mkdir ~/work</li>
<li>./configure –-prefix=$HOME/work/node</li>
<li>make</li>
<li>make install</li>
</ul>
<p>上面等它执行完毕就算安装成功了，然后设置下环境变量，这样就不用每次开启终端都要进入到特定目录才能执行node命令。(PS:环境变量设置非必要步骤，只是方便之后的使用、操作)</p>
<ul>
<li>echo &#8216;export PATH=$HOME/work/node/bin:$PATH&#8217; >> ~/.profile</li>
<li>echo &#8216;export NODE_PATH=$HOME/work/node:$HOME/work/node/lib/node_modules&#8217; >> ~/.profile</li>
<li>source ~/.profile</li>
</ul>
<p>到这里最基础的nodejs环境就搞定了，你可以执行&#8221;node -v&#8221;看下node版本号（前提是你设置了环境变量且更新了，要不你就得到bin目录下去执行了）。</p>
<h2>安装NPM,ExpressJS</h2>
<p>NPM是nodejs的包管理，帮你下载、管理一些nodejs相关组件什么的。我们先回到work目录，然后执行下面命令</p>
<ul>
<li>curl http://npmjs.org/install.sh | sh</li>
<li>npm install express -verbose</li>
</ul>
<p>先是安装npm，然后用npm下载配置express，-verbose参数用于查看详细的安装信息，如果在某个地方卡太久不动，就停了重新执行。命令成功后，我们进入work/node_modules/express目录执行<strong>npm install -d</strong>安装一些相关的依赖项，同理，如果卡太久不动，就重新多执行几次。</p>
<p>接下来依然是设置环境变量，把$HOME/work/node_modules/express/bin加到$PATH里，执行<strong>express -v</strong>能看到版本号就顺利安装成功了。</p>
<h2>安装mongoDB</h2>
<p>首先必须在系统根目录创建/data/db目录，步骤如下</p>
<ul>
<li>mkdir /data/db -p</li>
<li>chown &#8216;获取权限的用户名(一般是当前用户名)&#8217;  /data/db</li>
</ul>
<p>然后解压mongodb压缩包到work目录下(假设解压后的目录名为mongodb-2.0)，把<strong>$HOME/work/mongodb-2.0/bin</strong>添加到环境变量$PATH下。</p>
<p>最后我们试运行一下，执行<strong>mongod</strong>命令开启服务，新开终端执行下面命令(注意环境变量的问题)</p>
<ul>
<li>mongo</li>
<li>db.foo.save({a:&#8217;1&#8242;})</li>
<li>db.foo.findOne()</li>
</ul>
<p>如果没报什么错，到这里就算“搞定”啦！！</p>
<p style="margin:20px 0;">测试代码我就不写了，参考下面给出的网址就可以了。<strong>以上步骤nodejs+express的代码我试运行都可以正常运行，MongoDB就还没试过，只是在终端下可以操作而已，可能还需要配置些什么才能在代码里引用吧，这等做了实验后我再补上吧。</strong></p>
<p>相关网址</p>
<ul style=" margin-bottom:30px;">
<li><a href="http://cnodejs.org/blog/?p=2080">详解如何在ubuntu上安装nodejs</a></li>
<li><a href="http://nodejs.org/" title="NodeJS官网">NodeJS官网</a></li>
<li><a href="http://expressjs.com/" title="ExpressJS官网">ExpressJS官网</a></li>
<li><a href="http://www.mongodb.org/" title="mongoDB官网">mongoDB官网</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://pzling.com/2011/10/use-ubuntu-environment-to-build-nodejs-expressjs-mongodb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript微模板引擎</title>
		<link>http://pzling.com/2011/08/javascript-micro-templating/</link>
		<comments>http://pzling.com/2011/08/javascript-micro-templating/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 13:11:12 +0000</pubDate>
		<dc:creator>pzling</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[javascript 模板引擎]]></category>
		<category><![CDATA[js 模板引擎]]></category>
		<category><![CDATA[模板引擎]]></category>

		<guid isPermaLink="false">http://pzling.com/?p=732</guid>
		<description><![CDATA[Javascript微模板引擎，好吧，叫得这么好听其实就一简陋的前端模板。 后端模板大家应该听得比较多吧（Smarty，Velocity等），而前端我们通常是直接在JS里拼接我们需要的html，这种混合写在一起的方式，导致html结构稍微复杂点就很难看清楚，对维护造成一定的不便。所以人们就“发明”了模板这东西，使用一些特殊标记来代表数据循环、值替换等功能，使内容格式尽可能清晰。 模板的核心就是模板解析，下面要展示的引擎也仅有这个功能，没有其它扩展，要求不高的可以拿去试试！！下面我们来看一下该引擎的程序： // Javascript Micro Templating // pzling - http://pzling.com/ var JSMT = JSMT &#124;&#124; &#123;&#125;; &#40;function&#40;JSMT&#41;&#123; var tmplCaches = &#123;&#125;, dictDecode = &#123; &#34;quot&#34;: '&#34;', &#34;lt&#34;: &#34;&#60;&#34;, &#34;gt&#34;: &#34;&#62;&#34;, &#34;amp&#34;: &#34;&#38;&#34;, &#34;nbsp&#34;: &#34; &#34; &#125;; dictEncode = &#123; '&#34;': &#34;quot&#34;, &#34;&#60;&#34;: &#34;lt&#34;, &#34;&#62;&#34;: &#34;gt&#34;, &#34;&#38;&#34;: &#34;amp&#34;, &#34; &#34;: &#34;nbsp&#34; &#125;; &#160; /** * [...]]]></description>
			<content:encoded><![CDATA[<p>Javascript微模板引擎，好吧，叫得这么好听其实就一简陋的前端模板。</p>
<p>后端模板大家应该听得比较多吧（Smarty，Velocity等），而前端我们通常是直接在JS里拼接我们需要的html，这种混合写在一起的方式，导致html结构稍微复杂点就很难看清楚，对维护造成一定的不便。所以人们就“发明”了模板这东西，使用一些特殊标记来代表数据循环、值替换等功能，使内容格式尽可能清晰。</p>
<p class="box">模板的核心就是模板解析，下面要展示的引擎也仅有这个功能，没有其它扩展，要求不高的可以拿去试试！！下面我们来看一下该引擎的程序：</p>
<div class="more-wrap"><span id="more-732"></span></div>
<p class="box">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Javascript Micro Templating</span>
<span style="color: #006600; font-style: italic;">// pzling - http://pzling.com/</span>
<span style="color: #003366; font-weight: bold;">var</span> JSMT <span style="color: #339933;">=</span> JSMT <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>JSMT<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> tmplCaches <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		dictDecode <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;quot&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'&quot;'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;lt&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&lt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;gt&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;amp&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&amp;&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;nbsp&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		dictEncode <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'&quot;'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;quot&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&lt;&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;lt&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&gt;&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;gt&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&amp;&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;amp&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;nbsp&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * HTML解码
	 * @param {String} html
	 */</span>
	<span style="color: #003366; font-weight: bold;">function</span> htmlDecode<span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> String<span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&amp;(quot|lt|gt|amp|nbsp);/ig</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>all<span style="color: #339933;">,</span> key<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> dictDecode<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * HTML编码
	 * @param {String} html 
	 */</span>
	<span style="color: #003366; font-weight: bold;">function</span> htmlEncode<span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> String<span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[&quot;&lt;&gt;&amp; ]/g</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>all<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;&amp;&quot;</span> <span style="color: #339933;">+</span> dictEncode<span style="color: #009900;">&#91;</span>all<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * 模板解析函数
	 * @param {String} templating 模板字符
	 */</span>
	<span style="color: #003366; font-weight: bold;">function</span> analyze<span style="color: #009900;">&#40;</span>templating<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> _temp <span style="color: #339933;">=</span>templating.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&lt;#([\s\S]*?)#&gt;/g</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> s.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(\'|\\)/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\\</span>$1'</span><span style="color: #009900;">&#41;</span>
					.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[\r\n]/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span>
					.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;#'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;_s.push('&quot;</span><span style="color: #009900;">&#41;</span>
					.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#&gt;'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;');&quot;</span><span style="color: #009900;">&#41;</span>
					.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/{\$([\s\S]*?)\$}/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\'</span>,$1,<span style="color: #000099; font-weight: bold;">\'</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Function</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'var _s=[];'</span> <span style="color: #339933;">+</span> _temp <span style="color: #339933;">+</span> <span style="color: #3366CC;">';return _s'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * 模板注册函数
	 * @param {String} id 模板字符
	 * @param {String} data 模板字符
	 */</span>
	<span style="color: #003366; font-weight: bold;">function</span> register<span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> tar<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>id<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> id <span style="color: #339933;">===</span> <span style="color: #3366CC;">'object'</span> <span style="color: #339933;">&amp;&amp;</span> id.<span style="color: #660066;">tagName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			tar <span style="color: #339933;">=</span> id <span style="color: #339933;">;</span>
			id <span style="color: #339933;">=</span> tar.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			tar <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>tmplCaches<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			tmplCaches<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> analyze<span style="color: #009900;">&#40;</span>tar.<span style="color: #660066;">innerHTML</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">return</span> tmplCaches<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * 功能主调方法
	 * @param {String / Object} id 字符或dom对象
	 * @param {ALL} json 模板主要数据,任何数据类型都可以,模板使用this获取数据
	 */</span>
	JSMT.<span style="color: #660066;">render</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span>json<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> output <span style="color: #339933;">=</span> register.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>id<span style="color: #339933;">,</span>json <span style="color: #339933;">||</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> output<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #006600; font-style: italic;">/**
	 * 辅助方法
	 */</span>
	JSMT.<span style="color: #660066;">htmlEncode</span> <span style="color: #339933;">=</span> htmlEncode<span style="color: #339933;">;</span>
	JSMT.<span style="color: #660066;">htmlDecode</span> <span style="color: #339933;">=</span> htmlDecode<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>JSMT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</p>
<p class="box">程序对外提供三个方法，render是功能的主调方法，另外是两个辅助方法，用于html的编码与解码（防XSS）。下面再来看一下模板的定义方法（我拿DEMO里的模板来讲解）：</p>
<p class="box">

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;list&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/template&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item_tmpl1&quot;</span>&gt;</span>
            for(var i = 0 ,len = this.length ; i <span style="color: #009900;">&lt; len ; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">                &lt;# </span>
<span style="color: #009900;">                    &lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
                        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'{$ this[i].name $}'</span>&gt;</span>{$ this[i].name $}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
                        {$ JSMT.render(&quot;item_tmpl2&quot;,this[i].detail) $}
                    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span> 
                #&gt;
            }
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/template&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item_tmpl2&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;#</span>
<span style="color: #009900;">            &lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;margin-left:10px;&quot;</span>&gt;</span>Age:{$ this.age $}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;margin-left:10px;&quot;</span>&gt;</span>Gender:{$ JSMT.htmlDecode(this.gender) $}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;margin-left:10px;&quot;</span>&gt;</span>Hobby:{$ this.hobby$}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
        #&gt;
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

</p>
<p><strong><# #>：要输出的(html)内容；</strong></p>
<p><strong>{$ $}：要替换的变量，必须嵌套在<# #>标记内；</strong></p>
<p><strong>this：代替你传进来的数据；</strong></p>
<p class="box">PS：<strong>{$ $}只能是变量、函数调用，三元运算。</strong></p>
<p><strong>模板内容包含在&lt;script type=&#8221;text/template&#8221;&gt;标签内</strong>，type=&#8221;text/template&#8221;是我自定义的，因为系统识别不出这是什么type，所以它只直接忽略掉里面的内容。这里我使用了嵌套调用，在模板可以使用任意JS语法（你要定义一个函数都可以），当然一般只用 if 作逻辑处理还有 for 作循环。</p>
<p class="box">接下来是功能的调用：</p>
<p class="box">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
	<span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'list'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	container.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> JSMT.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>
		<span style="color: #3366CC;">'item_tmpl1'</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#91;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'marry'</span><span style="color: #339933;">,</span>
				detail<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
					age <span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
					gender  <span style="color: #339933;">:</span> <span style="color: #3366CC;">'&lt;span style=&quot;color:red;&quot;&gt;girl&lt;/span&gt;'</span><span style="color: #339933;">,</span>
					hobby <span style="color: #339933;">:</span> <span style="color: #3366CC;">'reading'</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Joe'</span><span style="color: #339933;">,</span>
				detail<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
					age <span style="color: #339933;">:</span> <span style="color: #CC0000;">23</span><span style="color: #339933;">,</span>
					gender  <span style="color: #339933;">:</span> <span style="color: #3366CC;">'&amp;lt;span&amp;nbsp;style=&quot;color:red;&quot;&amp;gt;girl&amp;lt;/span&amp;gt;'</span><span style="color: #339933;">,</span>
					hobby <span style="color: #339933;">:</span> <span style="color: #3366CC;">'playing'</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#93;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

</p>
<p>就这么简单，传入id（或dom）与数据（JSON或JSON数组，其实什么都行，看你模板里怎么写），返回解析后的内容。方法可多次调用，例如在数据翻页，传入不同数据调用。</p>
<p class="box">使用方法就上面这些，功能简单，不过在一些小项目还是可以用用的。现在JQuery与EXT等的模板引擎也都很强大的，写这个简陋的功能也仅仅是学习一下，以后再扩展其它功能</p>
<p>最近一直没什么状态，这功能也是每天写不到5分钟，拖到昨天才整完。前两天才开始配了下nodeJS的环境，看了些资料，终于又觉得有些小兴奋有点状态了，这几天多抽时间学一下，也希望下周可以顺利点吧。</p>
<div><a href="http://demo.pzling.com/jsmt/" title="Javascript微模板引擎" class="demo-view">View Demo</a></div>
]]></content:encoded>
			<wfw:commentRss>http://pzling.com/2011/08/javascript-micro-templating/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Duff&#8217;s Device优化循环性能</title>
		<link>http://pzling.com/2011/05/aboutduffs-device/</link>
		<comments>http://pzling.com/2011/05/aboutduffs-device/#comments</comments>
		<pubDate>Sun, 22 May 2011 10:13:39 +0000</pubDate>
		<dc:creator>pzling</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Duff's Device]]></category>
		<category><![CDATA[性能优化]]></category>

		<guid isPermaLink="false">http://pzling.com/?p=672</guid>
		<description><![CDATA[&#8220;Duff&#8217;s Device&#8221;是一个循环体展开技术，它使得一次迭代中实际执行了多次迭代的操作。它最早是在1983年C上由Tom Duff实现，然后2001年Jeff Greenberg移植到JavaScript上。 我们先来一个典型的实现方式： //credit: Jeff Greenberg var iterations = Math.floor&#40;items.length / 8&#41;, startAt = Items.length % 8, i = 0; do&#123; switch&#40;startAt&#41;&#123; case 0: process&#40;items&#91;i++&#93;&#41;; case 7: process&#40;items&#91;i++&#93;&#41;; case 6: process&#40;items&#91;i++&#93;&#41;; case 5: process&#40;items&#91;i++&#93;&#41;; case 4: process&#40;items&#91;i++&#93;&#41;; case 3: process&#40;items&#91;i++&#93;&#41;; case 2: process&#40;items&#91;i++&#93;&#41;; case 1: process&#40;items&#91;i++&#93;&#41;; &#125; &#125;while&#40;--iterations&#41;; &#8220;Duff&#8217;s Device&#8221;基本原理是：在每次循环中调用8次process()，减少迭代次数。首先先用循环总数除以8求余数放入startAt，表示第一次循环应调用多少次process()，之后每次都会执行8次process()。 下面是此算法一个略微优化的版本，取消了switch语句 //credit: Jeff [...]]]></description>
			<content:encoded><![CDATA[<p class="box">&#8220;Duff&#8217;s Device&#8221;是一个循环体展开技术，它使得一次迭代中实际执行了多次迭代的操作。它最早是在1983年C上由Tom Duff实现，然后2001年Jeff Greenberg移植到JavaScript上。</p>
<p>我们先来一个典型的实现方式：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//credit: Jeff Greenberg</span>
<span style="color: #003366; font-weight: bold;">var</span> iterations <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>items.<span style="color: #660066;">length</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
     startAt <span style="color: #339933;">=</span> Items.<span style="color: #660066;">length</span> <span style="color: #339933;">%</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">,</span>
     i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">do</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>startAt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">7</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">--</span>iterations<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<div class="more-wrap"><span id="more-672"></span></div>
<p>&#8220;Duff&#8217;s Device&#8221;基本原理是：在每次循环中调用8次process()，减少迭代次数。首先先用循环总数除以8求余数放入startAt，表示第一次循环应调用多少次process()，之后每次都会执行8次process()。</p>
<p>下面是此算法一个略微优化的版本，取消了switch语句</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//credit: Jeff Greenberg</span>
<span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> items.<span style="color: #660066;">length</span> <span style="color: #339933;">%</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
i <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>items.<span style="color: #660066;">length</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>如果迭代次数很非常多，性能问题比较大的情况下，各位可以尝试一下这个算法。至于可读性吗&#8230;&#8230;&#8230;.大家自己根据实际情况权衡。</p>
<p><a href="http://pzling.com/2010/08/timed-array-processing/" title="大数组分时处理">另一种优化大数组的方法</a></p>
<p style="margin-bottom:30px;">更多资料：<a href="http://en.wikipedia.org/wiki/Duff%27s_device">Duff&#8217;s device Wiki</a></p>
<p><strong>2011-08-19 更新</strong></p>
<p>上面提供的两段代码有几处明显错误，之前自己只看懂了思路，代码没细看就放出来了，在这里更正一下：</p>
<p class="box">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iterations <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>items.<span style="color: #660066;">length</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
     startAt <span style="color: #339933;">=</span> Items.<span style="color: #660066;">length</span> <span style="color: #339933;">%</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">,</span>
     i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">do</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>startAt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">7</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  startAt <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//这里从书上少抄了</span>
<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>iterations<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//可以改这里的条件，或者上面iterations的赋值</span></pre></div></div>

</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> t <span style="color: #339933;">=</span> items.<span style="color: #660066;">length</span> <span style="color: #339933;">%</span> <span style="color: #CC0000;">8</span> <span style="color: #339933;">,</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
t <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>items.<span style="color: #660066;">length</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</p>
<p>在此感谢二师兄指出问题哈！</p>
<p><a href="http://www.cnblogs.com/yi-y/archive/2011/08/19/2145229.html">http://www.cnblogs.com/yi-y/archive/2011/08/19/2145229.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pzling.com/2011/05/aboutduffs-device/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image.onload在IE下无法获取宽高的问题</title>
		<link>http://pzling.com/2011/05/img-onload-unable-to-get-width-and-height/</link>
		<comments>http://pzling.com/2011/05/img-onload-unable-to-get-width-and-height/#comments</comments>
		<pubDate>Mon, 16 May 2011 14:23:03 +0000</pubDate>
		<dc:creator>pzling</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[onload]]></category>
		<category><![CDATA[无法获取宽高]]></category>

		<guid isPermaLink="false">http://pzling.com/?p=666</guid>
		<description><![CDATA[今天在IE下想在onload事件获取img的宽高，但一直取不到值，就在网上搜索了一下，原来又是和顺序有关，XXX。我们先来看下面这个例子： var img = new Image&#40;&#41;; img.src = &#34;test.gif&#34;; img.onload = function&#40;&#41;&#123; alert &#40; img.width &#41;; &#125;; 单纯这样看，并没不会觉得有什么问题。但在IE中存在BUG，第一次打开页面时还正常，之后就没反应了，怎么刷新也无效，这是因为图片已经被缓存在本地了。 var img = new Image&#40;&#41;; img.onload = function&#40;&#41;&#123; alert &#40; img.width &#41;; &#125;; img.src = &#34;test.gif&#34;; 我们现在改成上面这样，问题就解决了。我们还可以通过img.complete先来判断是否已缓存在本地，如果是那就不用用onload去获取了 要注意的是在IE6下，GIF含有多帧的图片会多次加载，所以在onload事件里，记得要把img.onload设置为null]]></description>
			<content:encoded><![CDATA[<p>今天在IE下想在onload事件获取img的宽高，但一直取不到值，就在网上搜索了一下，原来又是和顺序有关，XXX。我们先来看下面这个例子：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;test.gif&quot;</span><span style="color: #339933;">;</span>
img.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span> <span style="color: #009900;">&#40;</span> img.<span style="color: #660066;">width</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>单纯这样看，并没不会觉得有什么问题。但在IE中存在BUG，第一次打开页面时还正常，之后就没反应了，怎么刷新也无效，这是因为图片已经被缓存在本地了。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
img.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span> <span style="color: #009900;">&#40;</span> img.<span style="color: #660066;">width</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;test.gif&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>我们现在改成上面这样，问题就解决了。我们还可以通过img.complete先来判断是否已缓存在本地，如果是那就不用用onload去获取了</p>
<p>要注意的是在IE6下，GIF含有多帧的图片会多次加载，所以在onload事件里，<strong>记得要把img.onload设置为null</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://pzling.com/2011/05/img-onload-unable-to-get-width-and-height/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iphone、ipad Web开发小技巧(知识)</title>
		<link>http://pzling.com/2011/02/iphone-mobile-web-development-tips/</link>
		<comments>http://pzling.com/2011/02/iphone-mobile-web-development-tips/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 16:34:41 +0000</pubDate>
		<dc:creator>pzling</dc:creator>
				<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Mobile Dev]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://pzling.com/?p=550</guid>
		<description><![CDATA[iphone、ipad都水果家的，区别也不是很大(别和我提屏幕!!!)，所以放一起不作过多区分，基本都通用。资料都网上收集来的，很多我也还没条件测试，可能有些知识点已经更新了。 如何隐藏iPhone Safari的地址栏 使用meta标签，看一些资料说的，不过我在iphone4下测试没反应，不清楚iphone3是否有效或者是有什么限制。 &#60;meta name=&#34;apple-mobile-web-app-capable&#34; content=&#34;yes&#34; /&#62; 第二个方法就是通过JS处理。原理就是当我们往下滚动时，地址栏会往上收起，所以我们重围一下默认位置就可以了。也因为是使用系统自身的特性，当内容小于可视区域时(没有滚动条)，地址栏是不能隐藏的。 window.addEventListener&#40;&#34;load&#34;, function&#40;&#41; &#123; setTimeout&#40;hideURLbar, 0&#41;; &#125;, false&#41;; function hideURLbar&#40;&#41;&#123; window.scrollTo&#40;0,1&#41;; &#125; 不同的方向对应不同的样式表 &#60;link rel=&#34;stylesheet&#34; media=&#34;all and (orientation:portrait)&#34; href=&#34;portrait.css&#34;&#62; &#60;link rel=&#34;stylesheet&#34; media=&#34;all and (orientation:landscape)&#34; href=&#34;landscape.css&#34;&#62; 设置可视窗的大小 &#60;meta name=&#34;viewport&#34; content=&#34;width=device-width,minimum-scale=1.0,maximum-scale=1.0&#34;/&#62; /*width - viewport的宽度 height - viewport的高度*/ /*initial-scale - 初始的缩放比例*/ /*minimum-scale - 允许用户缩放到的最小比例*/ /*maximum-scale - 允许用户缩放到的最大比例*/ /*user-scalable - 用户是否可以手动缩放*/ [...]]]></description>
			<content:encoded><![CDATA[<p style="margin-bottom:20px">iphone、ipad都水果家的，区别也不是很大(别和我提屏幕!!!)，所以放一起不作过多区分，基本都通用。资料都网上收集来的，很多我也还没条件测试，可能有些知识点已经更新了。</p>
<h2>如何隐藏iPhone Safari的地址栏</h2>
<p>使用meta标签，看一些资料说的，不过我在iphone4下测试没反应，不清楚iphone3是否有效或者是有什么限制。</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;apple-mobile-web-app-capable&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;yes&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>第二个方法就是通过JS处理。原理就是当我们往下滚动时，地址栏会往上收起，所以我们重围一下默认位置就可以了。也因为是使用系统自身的特性，<strong>当内容小于可视区域时(没有滚动条)，地址栏是不能隐藏的</strong>。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;load&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> setTimeout<span style="color: #009900;">&#40;</span>hideURLbar<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> hideURLbar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
window.<span style="color: #660066;">scrollTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>不同的方向对应不同的样式表</h2>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;all and (orientation:portrait)&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;portrait.css&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;all and (orientation:landscape)&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;landscape.css&quot;</span>&gt;</span></pre></div></div>

<div class="more-wrap"><span id="more-550"></span></div>
<h2>设置可视窗的大小</h2>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;viewport&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width=device-width,minimum-scale=1.0,maximum-scale=1.0&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
/*width - viewport的宽度 height - viewport的高度*/
/*initial-scale - 初始的缩放比例*/
/*minimum-scale - 允许用户缩放到的最小比例*/
/*maximum-scale - 允许用户缩放到的最大比例*/
/*user-scalable - 用户是否可以手动缩放*/</pre></div></div>

<p>这里提一下机械人(android)家的关于user-scalable的问题，2.1有效，但2.2是无效的</p>
<h2>设置网页的桌面快捷方式图标</h2>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;apple-touch-icon&quot;</span>  sizes<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;72x72&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;apple-touch-icon.png&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;apple-touch-startup-image&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;startup.png&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>第一行是设置桌面快捷方式图标的，没有sizes属性默认57*57像素的图片，圆角和高亮效果系统会自动帮你搞定，放在网页根目录会作用于下面的全部网页，当然也可以为每个页面设置单独的图标。</p>
<p>第二行是设置启动画面。</p>
<p><a href="http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html" title="Configuring Web Applications">相关资料</a></p>
<h2>单独给iphone设置样式</h2>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;only screen and (max-device-width: 480px)&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;iphone.css&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span>&gt;</span></pre></div></div>

<p><a href="http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/OptimizingforSafarioniPhone/OptimizingforSafarioniPhone.html" title="Optimizing Web Content">相关资料</a></p>
<h2>特殊链接</h2>
<p>当你浏览一个有电话号码的页面时，可以直接按号码就可以打出电话，这时电话号码自动变成链接，当然它们遵循电话号码的格式，但有时你可能要手工创建一个电话号码链接，这时你可以使用tel:前缀(URI模式)，如：</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tel:03545770051&quot;</span>&gt;</span>海健<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sms:03545770051&quot;</span>&gt;</span>短信<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<h2>在网页中检测屏幕的方向</h2>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">orientation</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">:</span>
    <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">90</span><span style="color: #339933;">:</span>
    <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">90</span><span style="color: #339933;">:</span>
    <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">180</span><span style="color: #339933;">:</span>
    <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>手势事件(guesture)</h2>
<p>在iPhone上，手势是两个手指的行为：缩放(放大和缩小)和旋转。iPhone支持如下几种手势事件：</p>
<ul>
<li>gesturestart</li>
<li>gestureend</li>
<li>gesturechange</li>
</ul>
<p>下面的例子中我们将监听gesturechange事件，然后使用webkitTransform样式属性缩放和旋转一个div，像往常一样，事件监听器接收event对象参数，event对象包含以下属性：</p>
<ul>
<li>event.scale ： 不缩放时值为1，缩小时值小于1，放大时值大于1</li>
<li>event.rotate ：旋转的角度</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    bstyle <span style="color: #339933;">=</span> b.<span style="color: #660066;">style</span><span style="color: #339933;">;</span>
    b.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gesturechange'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        bstyle.<span style="color: #660066;">webkitTransform</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'scale('</span> <span style="color: #339933;">+</span> event.<span style="color: #660066;">scale</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">') '</span> <span style="color: #339933;">+</span>
        <span style="color: #3366CC;">'rotate('</span><span style="color: #339933;">+</span> event.<span style="color: #660066;">rotation</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'deg)'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>其它小知识点</h2>
<p>iphone内置了一个调试控制台；</p>
<p>每个静态资源(*.html,*.css,*.js etc) < 10M ；</p>
<p>超过25K的资源不会被缓存(gzip前)；</p>
<p>5秒钟的javascript执行时间 [<a href="http://yuiblog.com/blog/2008/02/06/iphone-cacheability/" title="Performance Research, Part 5: iPhone Cacheability – Making it Stick">更多资料</a>]</p>
</p>
<p>-webkit-text-size-adjust设置页面文字尺寸，默认为auto，建议直接设置为none；</p>
<p>-webkit-tap-highlight-color  设置手指触发链接是的颜色</p>
<p style="margin-top:30px;">更多开发资料，请查看<a href="http://developer.apple.com/" title="Apple Developer">Apple Developer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pzling.com/2011/02/iphone-mobile-web-development-tips/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Javascript里的Array-Like对象</title>
		<link>http://pzling.com/2011/01/javascript-array-like-object/</link>
		<comments>http://pzling.com/2011/01/javascript-array-like-object/#comments</comments>
		<pubDate>Sun, 16 Jan 2011 16:26:36 +0000</pubDate>
		<dc:creator>pzling</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Array-Like]]></category>
		<category><![CDATA[slice]]></category>

		<guid isPermaLink="false">http://pzling.com/?p=506</guid>
		<description><![CDATA[通常我们用 [array].slice()去重组数组或转换数组，但对于arguments就无效了，尽管它有length属性，但它并不是一个真正的数组，而是一个Array-Like对象，所以像Array对象那样写会报&#8221;arguments.slice() is not a function&#8220;错误。这个时候我们就可以使用Array.prototype.slice.call来把arguments转换为真正的数组。 function foo&#40;&#41;&#123; var arr=Array.prototype.slice.call&#40;arguments, 0&#41;; arr.slice&#40;&#41;; //现在不会报错了，因为它是一个真正的数组 &#125; &#160; foo&#40;'pzling','http://pzling.com'&#41;; Array.prototype.slice.call()主要用于将方法内的arguments转换成数组，但是它有应用的局限，对于DOM节点集合，它是无能为力的。 很老的知识点，作下记录。 参考资料：Array-like Objects in JavaScript]]></description>
			<content:encoded><![CDATA[<p>通常我们用 [array].slice()去重组数组或转换数组，但对于arguments就无效了，尽管它有length属性，但它并不是一个真正的数组，而是一个Array-Like对象，所以像Array对象那样写会报&#8221;<strong>arguments.slice() is not a function</strong>&#8220;错误。这个时候我们就可以使用<strong>Array.prototype.slice.call</strong>来把arguments转换为真正的数组。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #003366; font-weight: bold;">var</span> arr<span style="color: #339933;">=</span>Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 arr.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//现在不会报错了，因为它是一个真正的数组</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
foo<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pzling'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'http://pzling.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Array.prototype.slice.call()主要用于将方法内的arguments转换成数组，但是它有应用的局限，对于DOM节点集合，它是无能为力的</strong>。</p>
<p>很老的知识点，作下记录。</p>
<div>参考资料：<a href="http://shifteleven.com/articles/2007/06/28/array-like-objects-in-javascript">Array-like Objects in JavaScript</a></div>
]]></content:encoded>
			<wfw:commentRss>http://pzling.com/2011/01/javascript-array-like-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

