用於相容瀏覽器的js寫法

來源:互聯網
上載者:User

標籤:als   trident   call   cli   寫法   scrollto   char   ie6   文檔   

用於引用的源檔案代碼:

var Common = {
getEvent: function() {//ie/ff
if (document.all) {
return window.event;
}
func = getEvent.caller;
while (func != null) {
var arg0 = func.arguments[0];
if (arg0) {
if ((arg0.constructor == Event || arg0.constructor == MouseEvent) || (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) {
return arg0;
}
}
func = func.caller;
}
return null;
},

getMousePos: function(ev) {//取得滑鼠位置
if (!ev) {
ev = this.getEvent();
}
if (ev.pageX || ev.pageY) {
return {
x: ev.pageX,
y: ev.pageY
};
}

if (document.documentElement && document.documentElement.scrollTop) {
return {
x: ev.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft,
y: ev.clientY + document.documentElement.scrollTop - document.documentElement.clientTop
};
}
else if (document.body) {
return {
x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y: ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
},

isIE : navigator.userAgent.toUpperCase().indexOf("MSIE")?true:false,

isFirefox : navigator.userAgent.toUpperCase().indexOf("FIREFOX")?true:false,

getIeVersion: function(){//取得ie版本
var userAgent = navigator.userAgent.toLowerCase();
if(userAgent.match(/msie ([\d.]+)/)!=null){//ie6--ie9
uaMatch = userAgent.match(/msie ([\d.]+)/);
return uaMatch[1];
}else if(userAgent.match(/(trident)\/([\w.]+)/)){
uaMatch = userAgent.match(/trident\/([\w.]+)/);
switch (uaMatch[1]){
case "4.0":
return 8;
break;
case "5.0":
return 9;
break;
case "6.0":
return 10;
break;
case "7.0":
return 11;
break;
default:
return -1;
}
}
return -1;
},

getById: function(id) {
return "string" == typeof id ? document.getElementById(id) : id;
}
}

儲存為common.js檔案

下面是引用此檔案的html文檔

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>
<input type="button" id="mousePos" value="滑鼠座標" />
<input type="button" onclick="showIeVersion();" value="IE版本" />

<script type="text/javascript" src="common.js"></script>
<script type="text/javascript">
document.getElementById("mousePos").onmousedown=function(e){
var ev = e || window.event || Common.getEvent();
var pos = Common.getMousePos(ev);
console.log(‘x:‘ + pos.x + ‘, y:‘ + pos.y);
}

function showIeVersion(){
var ieVer = parseInt(Common.getIeVersion());
if(ieVer==-1){
console.log(‘非IE瀏覽器‘);
}else{
console.log(‘IE‘ + ieVer);
}
}
</script>

</body>
</html>

 

用於相容瀏覽器的js寫法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.