javascript 自定义常用函数

1、获取元素的位置
说明:obj为DOM元素对象。返回位置对象(pos)
function getPos(obj) {
var pos = {
"top" : 0,
"left" : 0
};
if (obj.offsetParent) {
while (obj.offsetParent) {
pos.top += obj.offsetTop;
pos.left += obj.offsetLeft;
obj = obj.offsetParent;
}
} else if (obj.x) {
pos.left += obj.x;
} else if (obj.x) {
pos.top += obj.y;
}
return pos;
}

2、获取滚动位置
function getPageScroll(){
var scrollObj = {
"top" : 0,
"left" : 0
};
if (self.pageYOffset) {
scrollObj.top = self.pageYOffset;
scrollObj.left = self.pageXOffset;
} else if (document.documentElement && document.documentElement.scrollTop){
scrollObj.top = document.documentElement.scrollTop;
scrollObj.left = document.documentElement.scrollLeft;
} else if (document.body) {
scrollObj.top = document.body.scrollTop;
scrollObj.left = document.body.scrollLeft;
}
return scrollObj;
}

3、获取窗口尺寸
function getWindowSize()
{
var win = {
"width" : 0,
"height" : 0
};
//获取窗口宽度
if (window.innerWidth)
win.width = window.innerWidth;
else if ((document.body) && (document.body.clientWidth))
win.width = document.body.clientWidth;
//获取窗口高度
if (window.innerHeight)
   win.height = window.innerHeight;
else if ((document.body) && (document.body.clientHeight))
win.height= document.body.clientHeight;
//获取窗口大小
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)
{
win.height = document.documentElement.clientHeight;
win.width = document.documentElement.clientWidth;
}
return win;
}


4、触发事件
/**
* 触发事件
*
* @param el 对象
* @param eventName 事件名称
* @return
*/
function touchOffEvent(el, eventName) {
if (document.all) {
el.fireEvent(eventName);
} else {
if ("onchange" == eventName.toLowerCase()) {
el.onchange();
} else if ("onclick" == eventName.toLowerCase()) {
el.onclick();
} else if ("ondblclick" == eventName.toLowerCase()) {
el.ondblclick();
} else if ("onfocus" == eventName.toLowerCase()) {
el.onfocus();
} else if ("onselect" == eventName.toLowerCase()) {
el.onselect();
} else if ("onhelp" == eventName.toLowerCase()) {
el.onhelp();
} else if ("onkeydown" == eventName.toLowerCase()) {
el.onkeydown();
} else if ("onkeypress" == eventName.toLowerCase()) {
el.onkeypress();
} else if ("onkeyup" == eventName.toLowerCase()) {
el.onkeyup();
} else if ("onmousedown" == eventName.toLowerCase()) {
el.onmousedown();
} else if ("onmousemove" == eventName.toLowerCase()) {
el.onmousemove();
} else if ("onmouseout" == eventName.toLowerCase()) {
el.onmouseout();
} else if ("onmouseover" == eventName.toLowerCase()) {
el.onmouseover();
} else if ("onmouseup" == eventName.toLowerCase()) {
el.onmouseup();
} else {
alert("没有"+eventName+"事件!");
}
}
}

5、检查用户使用的浏览器

/**
* 检查用户使用的浏览器
*
* @return
*/
function checkUserAgent() {
if ((navigator.userAgent.indexOf('MSIE') >= 0)
&& (navigator.userAgent.indexOf('Opera') < 0)) {
alert('你是使用IE');
} else if (navigator.userAgent.indexOf('Firefox') >= 0) {
alert('你是使用Firefox');
} else if (navigator.userAgent.indexOf('Opera') >= 0) {
alert('你是使用Opera');
} else {
alert('你是使用其他的浏览器浏览网页!');
}

}

6、去除两端空格
function trim(str) {
return triml(trimr(str));
}

//去除右边空格
function trimr(str){
return (str + '').replace(/(\s+)$/g, '');
}
//去除左边空格
function triml(str) {
return (str + '').replace(/^\s+/g, '');
}


7、email正则表达式验证
function checkEmail(val){
if(/^[a-zA-Z]([a-zA-Z0-9]*[-_.]?[a-zA-Z0-9]+)+@([\w-]+\.)+[a-zA-Z]{2,}$/.test(val+"")){
return true;
}else{
return false;
}
}

8、复制内容到剪切板
function copyMessage(mess){
if(window.clipboardData){
window.clipboardData.setData("Text", mess);
alert("己复制到剪切板");
return true;
}else if(window.netscape){
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("您的firefox安全限制限制您进行剪贴板操作,请打开'about:config'将signed.applets.codebase_principal_support'设置为true'之后再重试!");
return false;
}
var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
if (!clip){
alert("当前浏览器不支持,请手动复制");
return false;
}
var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
if (!trans){
alert("当前浏览器不支持,请手动复制");
return false;
}
trans.addDataFlavor('text/unicode');
var str = new Object();
var len = new Object();
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
var copytext = mess;
str.data = copytext;
trans.setTransferData("text/unicode",str,copytext.length*2);
var clipid = Components.interfaces.nsIClipboard;
if (!clip)
return false;
clip.setData(trans,null,clipid.kGlobalClipboard);
alert("己复制到剪切板");
}else if(navigator.userAgent.indexOf("Opera")!= -1){
window.location = mess;
alert("己复制到剪切板");
}
}

你可能感兴趣的:(JavaScript,正则表达式,浏览器,Opera,firefox)