用于引用的源文件代码:

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写法的更多相关文章

  1. 兼容多数浏览器的js添加收藏夹脚本

    浏览器不断发展,js的很多脚本需要跟进才能适应,目前多数代码对新版本浏览器(IE11, Firefox 27)无法适用,特关注跟进. 推荐代码1 适用浏览器:IE11(windows 8.1), Fi ...

  2. 一个兼容 node 与浏览器的模块写法

    一个兼容 node 与浏览器的模块写法 // test.js (function (root, factory) { if (typeof define === 'function' &&am ...

  3. js 事件监听 兼容浏览器

    js 事件监听 兼容浏览器   ie 用 attachEvent   w3c(firefox/chrome)  用 addEventListener 删除事件监听 ie 用 detachEven   ...

  4. js event 事件兼容浏览器 ie不需要 event参数 firefox 需要

    js event 事件兼容浏览器    ie不需要 event参数   firefox 需要 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...

  5. 下面css hack的写法分别用于哪些浏览器

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  6. 如何写兼容浏览器和Node.js环境的Javascript代码

    如果有打开过jQuery的源码(从1.11及以后),或者Vue.js.React.js的源码,都会在文件的前面看见这样一段代码: ( function( global, factory ) { &qu ...

  7. 主流浏览器Css&js hack写法

    参考: BROWSER HACKS 主流浏览器的Hack写法

  8. 兼容各种浏览器的hack写法

    1.Firefox @-moz-document url-prefix() { .selector { property: value; } }上面是仅仅被Firefox浏览器识别的写法 具体如:@- ...

  9. JS window对象 Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本。

    Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 对象属性: 查看浏览器的名称和版本,代码如下: <script type=" ...

随机推荐

  1. 【算法学习】老算法,新姿势,STL——Heap

    “堆”是一个大家很熟悉的数据结构,它可以在\(O(log\;n)\)的时间内维护集合的极值. 这都是老套路了,具体的内部实现我也就不谈了. 我一般来说,都是用queue库中的priority_queu ...

  2. 2017-2018-2 20179205《网络攻防技术与实践》Windows攻击实验

    Windows攻击实验 实验描述: 使用Metaspoit攻击MS08-067,提交正确得到远程shell过程的截图(不少于五张). MS08-067漏洞介绍   MS08-067漏洞的全称为&quo ...

  3. SqlServer存储过程(增删改查)

    * IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值. CREATE PROCEDURE [dbo].[PR_NewsAffiche_AddNewsEntity] ( ...

  4. iBt(001-004)原文与试译

    Unit 001 Basic building materials include: timber, mud, stone, marble, brick, tile, steel, and cemen ...

  5. 使用数据库管理工具打开MySql

    1.推荐使用软件:Navicat_Premium_11.0.10.exe.  下载地址:http://pan.baidu.com/s/1nu6mTF7 2.下载上面文件并安装. 3.打开Navicat ...

  6. CentOS系统时间与现在时间相差8小时解决方法

    很多网友在安装完CentOS系统后发现时间与现在时间相差8小时,这是由于我们在安装系统的时选择的时区是上海,而CentOS默认bios时间是utc时间,所以时间相差了8小时.这个时候的bios的时间和 ...

  7. 在jsp页面中设置了远程验证,在初始化时必须预先调用一次。

    参考链接:http://code.taobao.org/p/sztaotao/diff/5/trunk/code/src/main/webapp/webpage/modules/sys/roleFor ...

  8. 学习记录:mongodb里插入整型值

    =============================================== 2018/1/24_第1次修改                       ccb_warlock == ...

  9. 15 Defer, Panic, and Recover

    Defer, Panic, and Recover 4 August 2010 Go has the usual mechanisms for control flow: if, for, switc ...

  10. vue总结 08状态管理vuex

      状态管理 类 Flux 状态管理的官方实现 由于状态零散地分布在许多组件和组件之间的交互中,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue 提供 vuex:我们有受到 Elm 启发的状态管 ...