分类: JS/AJAX/DOM

浏览器本地存储(1)

浏览器存储方式主要下面几种:

  • Cookie
    4096字节;缺点:存储量小,请求头附带cookie带来性能问题。
  • Flash Share Object
    默认支持100k,跨浏览器存储最好的方式,毕竟现在没有安装flash插件的用户是很少的;缺点:需要加载一个swf文件,个别用户不支持flash。
    有一个基于jQuery的插件叫 jStore ,就是通过Flash Share Object实现的。
  • IE的 UserData
    最少也能支持640k,IE8后已经支持DOM Storage;缺点:IE only。
  • DOM Storage
    默认支持5M存储量;缺点:IE7,IE6不支持。
  • Google Gears
    功能最强;缺点:但需要安装软件,而且,安装的用户是较少的。

这里主要学习 IE 的 UserData 和 DOM Storage
阅读全文 »

Javascript Array 性能测试

array.push优化

  • 原生的方法arr.push()
  • 通过arr[arr.length]直接赋值
  • 把方法二写成一个函数方便调用

测试结果请猛击这里:
/examples/2010/03/array-performance/push.html

测试结果:

  • 给arr[arr.length]直接赋值比push方法快
  • 函数调用增加了开销,特别是在IE6和opera下表现明显
  • Chrome应该是做了优化,相差不大

待续(也许) =,=…….

TextArea光标位置插入文字

需要实现:在光标位置处插入文字,以及获取焦点后光标位置处于文字末尾。
以前一直没有做过,今天了解了一下后实现并记录在此。

各浏览器TextArea获得焦点后的光标位置情况:

textarea.focus()
  • FireFox: 所有文字结束处
  • IE: 文字开头
  • Opera: 文字开头
  • Chrome: 文字开头
  • Safari: 文字开头

IE支持document.selection
Firefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd属性
针对浏览器的特性进行判断并实现,代码如下:

function insertText(obj,str) {
	if (document.selection) {
		var sel = document.selection.createRange();
		sel.text = str;
	} else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
		var startPos = obj.selectionStart,
			endPos = obj.selectionEnd,
			cursorPos = startPos,
			tmpStr = obj.value;
		obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
		cursorPos += str.length;
		obj.selectionStart = obj.selectionEnd = cursorPos;
	} else {
		obj.value += str;
	}
}
function moveEnd(obj){
	obj.focus();
	var len = obj.value.length;
	if (document.selection) {
		var sel = obj.createTextRange();
		sel.moveStart('character',len);
		sel.collapse();
		sel.select();
	} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
		obj.selectionStart = obj.selectionEnd = len;
	}
}

Demo猛击这里

关于IE TextRange 对象的方法与属性介绍:
http://msdn.microsoft.com/zh-cn/library/ms535872%28en-us,VS.85%29.aspx

TextArea对象的属性:https://developer.mozilla.org/en/DOM/TextArea

小技巧,跨域关闭iframe

示例

DEMO猛击这里

解决方法

直接看代码吧:

function Dialog(){
	this.panel = document.createElement('div');
	this.panel.id = 'bmPanel';
	this.panel.style.position = 'absolute';
	this.panel.style.right = '0px';
	this.panel.style.right = this.scrollTop() + 'px';
	this.panel.style.zIndex = 100000;
	this.panel.style.margin = '10px';
	this.panel.style.width = '200px';
	this.panel.style.height = '200px';
	this.panel.style.border = '5px solid #CCC';
	this.panel.innerHTML = '<iframe id="bmIframe" '
		+ 'token="1" onload="c = this.getAttribute(\'token\'); if (c==3) {this.parentNode.parentNode.removeChild(this.parentNode)} this.setAttribute(\'token\',++c);"'
		+ 'name="bmIframe" src="main.html" scrolling="no" frameborder="0" style="width:100%; height:100%; '
		+ 'border:1px; padding:0px; margin:0px"></iframe>';
}

Dialog.prototype.show = function(){
	document.body.appendChild(this.panel);
}

Dialog.prototype.scrollTop = function (){
	return document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
}

注意iframe这一行:

onload="c = this.getAttribute(\'token\'); if (c==3) {this.parentNode.parentNode.removeChild(this.parentNode)} this.setAttribute(\'token\',++c);"

iframe里的关闭按钮链接一个简单的页面,通过不停地提交表单触发load事件,让token累加:

<body onload="document.getElementById('close').submit()">
	<form id="close" action="closeing.html" method="get"></form>
</body>

玩转嘀咕网的嘀咕秀(Widget)

一直郁闷嘀咕网没有开发JS的博客插件,今天终于看到这款APP出来啦,嘀咕秀:http://www.digushow.com
一共提供了三种Widget,Flash版的,图片版,以及JS版。
图片版一般是用于论坛签名,效果如下:

Foxling's digu

其实,任何支持HTML代码,或者支持图片的地方,都可以加上你的图片签名,如果你要在你的网页中插入图片签名,只需要添加如下代码:

<img src="http://digushow.com/sign/foxling/bbs_sign.png" alt="Foxling's digu" />

其中,foxling改为你的用户名,alt的属性改成你的说明。
如果是要在支持UBB代码的论坛里做签名,使用下面的代码:

[img]http://digushow.com/sign/foxling/bbs_sign.png[/img]

阅读全文 »