日期:2014-05-16  浏览次数:20299 次

JS 常用技术

java中URL 的编码和解码函数
java.net.URLEncoder.encode(String s)和java.net.URLDecoder.decode(String s);

在javascript 中URL 的编码和解码函数
escape(String s)和unescape(String s) ;

?


escape,encodeURI,encodeURIComponent函数比较

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>');</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

将服务器返回的字符串转换成json;
var str = "{name:'zhangxing'}";
var obj = eval("(" + str + ")");
alert(obj.name);

实用技巧:
???????
定时
??????? timer=setInterval('scrollwindow()',delay);
??????? clearInterval(timer);
UNCODE编码
??????? escape() ,unescape
交换表的行
??????? TableID.moveRow(2,1)
并排显示
??????? display:inline
隐藏焦点
??????? hidefocus=true
根据宽度换行
??????? style="word-break:break-all"
快速转到位置
??????? obj.scrollIntoView(true)
双字节字符
??????? /[^\x00-\xff]/
汉字
??????? /[\u4e00-\u9fa5]/
让英文字符串超出表格宽度自动换行
??????? word-wrap: break-word; word-break: break-all;
获得style内容
??????? obj.style.cssText
第一个style标签
??????? document.styleSheets[0]
style标签里的第一个样式
??????? document.styleSheets[0].rules[0]
防止点击空链接时,页面往往重置到页首端。
??????? <a href="javascript:function()">word</a>
禁止右键
??????? document.oncontextmenu = function() { return false;}
关闭输入法
??????? <input style="ime-mode:disabled">
获取选中内容
??????? document.selection.createRange().duplicate().text
统一编码/解码
??????? alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
??????? encodeURIComponent对":"、"/"、";" 和 "?"也编码

//各种尺寸

s? +=? "\r\n网页可见区域宽:"+? document.body.clientWidth;?
s? +=? "\r\n网页可见区域高:"+? document.body.clientHeight;?
s? +=? "\r\n网页可见区域高:"+? document.body.offsetWeight? +"? (包括边线的宽)";?
s? +=? "\r\n网页可见区域高:"+? document.body.offsetHeight? +"? (包括边线的宽)";?
s? +=? "\r\n网页正文全文宽:"+? document.body.scrollWidth;?
s? +=? "\r\n网页正文全文高:"+? document.body.scrollHeight;?
s? +=? "\r\n网页被卷去的高:"+? document.body.scrollTop;?
s? +=? "\r\n网页被卷去的左:"+? document.body.scrollLeft;?
s? +=? "\r\n网页正文部分上:"+? window.screenTop;?
s? +=? "\r\n网页正文部分左:"+? window.screenLeft;?
s? +=? "\r\n屏幕分辨率的高:"+? window.screen.height;?
s? +=? "\r\n屏幕分辨率的宽:"+? window.screen.width;?
s? +=? "\r\n屏幕可用工作区高度:"+? window.screen.availHeight;?
s? +=? "\r\n屏幕可用工作区宽度:"+? window.screen.availWidth;??
过滤数字
<input type=text onkeypress="return event.keyCode>=48&&event.keyCode<=57||(this.value.indexOf('.')<0?event.keyCode==46:false)" onpaste="return !clipboardData.getData('text').match(/\D/)" ondragenter="return false">
不缓存
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="0">

function cc()
{
var e = event.srcElement;
var r =e.createTextRange();
r.moveStart('character',e.value.length);
r.collapse(true);
r.select();
}

//刷新后不变的文本框 <HTML>
<HEAD>
<META NAME="save" CONTENT="his