用于兼容浏览器的js写法
用于引用的源文件代码:
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写法的更多相关文章
- 兼容多数浏览器的js添加收藏夹脚本
浏览器不断发展,js的很多脚本需要跟进才能适应,目前多数代码对新版本浏览器(IE11, Firefox 27)无法适用,特关注跟进. 推荐代码1 适用浏览器:IE11(windows 8.1), Fi ...
- 一个兼容 node 与浏览器的模块写法
一个兼容 node 与浏览器的模块写法 // test.js (function (root, factory) { if (typeof define === 'function' &&am ...
- js 事件监听 兼容浏览器
js 事件监听 兼容浏览器 ie 用 attachEvent w3c(firefox/chrome) 用 addEventListener 删除事件监听 ie 用 detachEven ...
- js event 事件兼容浏览器 ie不需要 event参数 firefox 需要
js event 事件兼容浏览器 ie不需要 event参数 firefox 需要 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- 下面css hack的写法分别用于哪些浏览器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 如何写兼容浏览器和Node.js环境的Javascript代码
如果有打开过jQuery的源码(从1.11及以后),或者Vue.js.React.js的源码,都会在文件的前面看见这样一段代码: ( function( global, factory ) { &qu ...
- 主流浏览器Css&js hack写法
参考: BROWSER HACKS 主流浏览器的Hack写法
- 兼容各种浏览器的hack写法
1.Firefox @-moz-document url-prefix() { .selector { property: value; } }上面是仅仅被Firefox浏览器识别的写法 具体如:@- ...
- JS window对象 Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本。
Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 对象属性: 查看浏览器的名称和版本,代码如下: <script type=" ...
随机推荐
- [转]STL 容器一些底层机制
1.vector 容器 vector 的数据安排以及操作方式,与 array 非常相似.两者的唯一区别在于空间的运用的灵活性.array 是静态空间,一旦配置了就不能改变,vector 是动态数组.在 ...
- C++面试常见问题
转载:https://zhuanlan.zhihu.com/p/34016871?utm_source=qq&utm_medium=social 1.在C++ 程序中调用被C 编译器编译后的函 ...
- spring boot 测试用例
junit 是一个面向于研发人员使用的轻量的测试模块,适合做单元测试.而testng百度后发现更强大,可以做功能测试,但对于我这种RD,貌似junit足沟了呢! java Mock PowerMock ...
- Oracle11g用户、权限、角色、概要文件管理及审计
第10章 安全管理 1 用户管理 2 权限管理 3 角色管理 : 4 概要文件管理 5 审计 操作系统:win7 Oracle安装目录:E盘 数据库名字:orcl 密码:123456 先 ...
- mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by
在mysql 工具 搜索或者插入数据时报下面错误: ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause ...
- 无需编译app切换线上、测试环境
在咱们测试过程中,经常需要切换测试环境和线上环境.大致有如下几个方案. 一.服务器地址编译到app中 此种方式需要在代码里保存两套配置,一套指向线上,一套指向测试.通过编译参数分别生成测试包.线上包. ...
- 使用dos命令创建多模块Maven项目
好吧,咱们接着上一篇博客继续用另一种方式来创建Maven项目.不过在创建之前我们应该先熟悉一些相关dos命令. 创建web项目命令: mvn archetype:generate -DgroupId= ...
- JS调用百度地图API标记地点
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- python 删除前3天的文件
一.需求分析 1. 删除前3天的文件 2.如果目录为空,也一并删除掉 如果使用shell脚本,一条命令就搞定了.干啥还要用python? 1. 因为需要记录一些日志,使用shell不好实现 2. 作为 ...
- #Git 详细中文安装教程
Step 1 Information 信息 Please read the following important information before continuing 继续之前,请阅读以下重要 ...