js的常用函数
1. 获取元素坐标
var getPos:function(o){//取元素坐标
var x = 0, y = 0;
do{
x += o.offsetLeft;
y += o.offsetTop;
}while(o=o.offsetParent);
return {'x':x,'y':y};
}
2. 获取当前script文件的路径
var strJsPath='';
(function(){
try {
throw Error("获取路径")
}catch(ex){
if(ex.fileName)//Firefox
strJsPath=ex.fileName
else if(ex.stack)//Chrome
strJsPath=(ex.stack.match(/at\s+(.*?):\d+:\d+/)||['',''])[1];
else if(ex.sourceURL)//Safari
strJsPath=ex.sourceURL
}
if(strJsPath)return; //其实所有浏览器都可以用下面的
var scripts=document.getElementsByTagName("script"),script=scripts[scripts.length-1];
strJsPath=document.querySelector?script.src:script.getAttribute("src",4)//IE8直接.src
})()
alert(strJsPath);3. 获取当前script中的参数
var getScriptArgs=function(){//获取多个参数
var scripts=document.getElementsByTagName("script"),
script=scripts[scripts.length-1],//因为当前dom加载时后面的script标签还未加载,所以最后一个就是当前的script
src=script.src,
reg=/(?:\?|&)(.*?)=(.*?)(?=&|$)/g,
temp,res={};
while((temp=reg.exec(src))!=null) res[temp[1]]=decodeURIComponent(temp[2]);
return res;
};
var args=getScriptArgs();
alert(args.a+" | "+args.b+" | "+args.c);
//假如上面的js是在这个js1.js的脚本中< script type="text/javascript" src="js1.js?a=abc&b=汉字&c=123">< /script>
var getScriptArg=function(key){//获取单个参数
var scripts=document.getElementsByTagName("script"),
script=scripts[scripts.length-1],
src=script.src;
return (src.match(new RegExp("(?:\\?|&)"+key+"=(.*?)(?=&|$)"))||['',null])[1];
};
alert(getScriptArg("c"));
4.控制上传file的文件类型
<input type="file" onchange="checkExt(this)"/>
<script type="text/javascript">
var checkExt=function(file) {
if(!(/(?:jpg|gif)$/i.test(file.value))) {
alert("只允许上传jpg和gif的图片");
if(window.ActiveXObject) {//for IE
file.select();//select the file ,and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
file.type="text";file.type="file";
} else file.value="";//for FF,Chrome,Safari
} else {
alert("ok");//or you can do nothing here.
}
};
</script>
5.js获取url中的参数
//lastest:
var getArgs=function() {//get url querystring
var params=document.location.search,reg=/(?:^\?|&)(.*?)=(.*?)(?=&|$)/g,temp,args={};
while((temp=reg.exec(params))!=null) args[temp[1]]=decodeURIComponent(temp[2]);
return args;
};
//只取一个:
var queryString=function(key){
return (document.location.search.match(new RegExp("(?:^\\?|&)"+key+"=(.*?)(?=&|$)"))||['',null])[1];
}
var args=getArgs();
alert(args.name+" | "+args.sex+" | "+args.age);
//测试链接:< a href="?name=abc&sex=男&age=12">test getQueryString
//deprecated
var getArgs=function() {
var params=document.location.search.substr(1),reg=/(?:^|&)(.*?)=(.*?)(?:&|$)/g,temp,args={};
while((temp=reg.exec(params))!=null) {
args[temp[1]]=temp[2];
reg.lastIndex-=1;//减1,下一次从结束的&开始匹配
}
return args;
};
var getArgs=function(){
var sarg = document.location.search.substr(1),rv={};
rv.filename = document.location.pathname.replace(/.*\//ig,'');
if(!sarg){return rv;}
var aarg = sarg.split('&'),atmp=[];
for(var i=0;i< aarg.length;i++){
atmp = aarg[i].split('=');
rv[atmp[0]] = unescape(atmp[1]);
}
return rv;
};
6.js Unicode编码转换
/*
*js Unicode编码转换
*/
var decToHex = function(str) {
var res=[];
for(var i=0;i < str.length;i++)
res[i]=("00"+str.charCodeAt(i).toString(16)).slice(-4);
return "\\u"+res.join("\\u");
}
var decToHex2 = function(str) {
var res="",c;
for(var i=0;i < str.length;i++){
c=str.charCodeAt(i);
res+=(c< 32 || c==38 || c>127)?"\\u"+c.toString(16):str.charAt(i)
}
return res
}
var hexToDec = function(str) {
str=str.replace(/\\/g,"%");
return unescape(str);
}
var str=decToHex2("decToHex unicode 编码转换");
alert("编码后:"+str+"\n\n解码后:"+hexToDec(str));
7.js HTML编码转换
/*
*js HTML Encode
*/
var htmlEncode=function(str) {//HTML des encode.
var res=[];
for(var i=0;i < str.length;i++)
res[i]=str.charCodeAt(i);
return "&#"+res.join(";&#")+";";
};
var htmlEncode2=function(s) {
var r = "", c;
for (var i = 0; i < s.length; i++) {
c = s.charCodeAt(i);
r += (c < 32 || c == 38 || c > 127) ? ("&#" + c + ";") : s.charAt(i);
}
return r;
};
//s.replace(/([\u4e00-\u9fa5]+)/g,function($,$1) {
// return htmlEncode($1);
//})
var htmlHexEncode=function(str) {//HTML hex encode.
var res=[];
for(var i=0;i < str.length;i++)
res[i]=str.charCodeAt(i).toString(16);
return "&#"+String.fromCharCode(0x78)+res.join(";&#"+String.fromCharCode(0x78))+";";//x ,防止ff下&#x 转义
};
var htmlDecode = function(str) {
return str.replace(/&#(x)?([^&]{1,5});?/g,function($,$1,$2) {
return String.fromCharCode(parseInt($2 , $1 ? 16:10));
});
};
var s="htmlEncode HTML 编码转换 &#";
var s1=htmlEncode(s)+"\n\n只对双字节和&编码:"+htmlEncode2(s);
var s2=htmlDecode(s1);
alert("编码前:"+s+"\n\n编码后:"+s1+"\n\n解码后:"+s2);
var s="htmlHexEncode HTML 编码转换&#";
var s1=htmlHexEncode(s);
var s2=htmlDecode(s1);
alert("编码前:"+s+"\n\n编码后:"+s1+"\n\n解码后:"+s2);
8. js 全半角转换
/*
*全角空格为12288,半角空格为32
*其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
*/
function SBC(text) {
return text.replace(/[\x20-\x7e]/g, function($) {
return $ == " " ? "\u3000" : String.fromCharCode($.charCodeAt(0) + 0xfee0);
});
}
var tODBC=function(input) {//to DBC case
var res="",c;
for(var i=0;i< input.length;i++) {
c=input.charCodeAt(i);
if(c >=0xFF01 && c <=0xFF5E)// 65281 65374
res+=String.fromCharCode(c-0xFEE0);//65248
else if(c==0x3000)//12288
res+=String.fromCharCode(0x20);//32
else
res+=input.charAt(i);
}
return res;
}
var toSBC=function(input) {//to SBC case
var res="",c;
for(var i=0; i < input.length;i++) {
c=input.charCodeAt(i);
if(c>=0x21 && c<=0x7e)//33 126
res+=String.fromCharCode(c+0xFEE0);
else if(c==0x20)
res += String.fromCharCode(0x3000);
else
res += input.charAt(i);
}
return res;
}