9月 212011
 

js 对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent

1、   传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。                           

例如:<script language="javascript">document.write(‘<a href="http://passport.baidu.com/?logout&aid=7& u=’+encodeURIComponent("http://cang.baidu.com/bruce42")+’">退出</a& amp; gt;’);</script>

2、   进行url跳转时可以整体使用encodeURI

例如:Location.href="/encodeURI"("http://cang.baidu.com/do/s?word=百度&ct=21");

3、   js使用数据时可以使用escape

例如:搜藏中history纪录。

4、   escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。

最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

encodeURI不编码字符有82个:!,#,$,&,’,(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

encodeURIComponent不编码字符有71个:!, ‘,(,),*,-,.,_,~,0-9,a-z,A-Z

 Posted by at 上午 6:05
6月 072011
 

(function(me){ // 这种方法参数写什么都行,只是不要覆盖全局的那个你不想覆盖的变量就行
    var a,b,c;
    me.ext_func = function(){…};
})(obj);


(function(){  // 这种方法,里面使用this就行了
   var a, b, c;
   this.ext_func = function() {…};
}).call(obj);

 Posted by at 上午 9:41
4月 102011
 

js中创建正则表达式对象有两种方法:

  1.  var reg = /pattern/;
  2.  var reg = new RegExp(‘pattern’)

如果 pattern是动态构造出来的,则第一种方式就不行了,如:
var reg = "/" + a_value + "/";
这个reg是字符串,不是正则表达式对象。这时候就需要使用第二种方式了。

使用第二种方式的时候也要注意,不要收第一种方式的影响,也不要受PHP中写法的影响,注意:
第二种方式创建的时候是不能有前后的反斜线的; // 这篇文章就说了这一句有用的话
参考资料:
http://www.cainiao8.com/web/js_note/js_regular_expression.html

 Posted by at 上午 9:39
9月 172010
 

 Posted by at 上午 2:37
8月 082010
 

url:  http://phpor.net/render_js.php?callback=cb
返回:
(function(){ if(‘cb’ && cb) { var call = cb; var json ={…}; call(json); } })();

一般来讲,写为:
cb({…});
就够了,这样的话,cb为空或者该函数不存在就会出现js错误

或者,写成如下:
if(‘cb’ && cb) { var call = cb; var json ={…}; call(json); }

不觉得这种方式和第一种有什么区别。

 Posted by at 上午 4:20
6月 192010
 

大致的原则是在js中基本别使用use strict, 比较详细的说明:
http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/

目前使用的scmascript 为4, 更多参考:
http://www.ecmascript.org/
http://zh.wikipedia.org/zh/ECMAScript

 Posted by at 上午 9:41