JavaScript、IEと非IEの違い


var isIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1 ? true : false;
var isIE9 = /msie\s+9/i.test(navigator.userAgent);

//1.float
if(isIE){
	document.getElementById("block").style.styleFloat = "right";
}else{
	document.getElementById("block").style.cssFloat = "right";
}

//2.      
if(isIE){
	var currentStyle = document.getElementById("block").currentStyle.backgroundColor;
	console.log(currentStyle);
}else{
	var dom = document.getElementById("block");
	var currentStyle = document.defaultView.getComputedStyle(dom,null).backgroundColor;
	console.log(currentStyle);
}

//3.      
if(isIE){
	var dom = document.getElementById("block");
	dom.onmouseover = function(event){
      if(isIE9){
				console.log(event.pageX);
				return;
			}
			event = event || window.event;
			console.log(event.clientX);
	}
}else{
	var dom = document.getElementById("block");
	dom.onmouseover = function(event){
		console.log(event.pageX);
	}
}

//4.             
if(isIE){
	var browerSize = {};
	browerSize.screenX = document.documentElement.clientWidth;
	browerSize['screenY'] = document.documentElement.clientHeight;
	console.log(browerSize);
}else{
	var browerSize = {};
	browerSize.screenX = window.innerHeight;
	browerSize['screenY'] = window.innerWidth;
}

//5.    
if(isIE){
	var elem = document.getElementById("hit");
	elem.attachEvent("onclick",function(event){
		event = event || window.event;
		//   
		console.log(event.srcElement);
		//    
		event.cancelBubble = true;
		//      
		event.returnValue = false;
	});
}else{
	var elem = document.getElementById("hit");
	elem.addEventListener("click",function(event){
		//   
		console.log(event.target && event.srcElement);
		//    
		event.stopPropagation();
		//      
		event.preventDefault();
	},false);
}

//6.           ,IE:parentElement
var parent = function(elem){
  elem = document.getElementById(elem);
	return elem.parentElement && elem.parentNode;
}

//7.              HTML    TextNode     
var getChildren = function(elem){
	elem = document.getElementById(elem);
	return elem.childNodes && elem.children;
}

//8.DOM    
if(isIE){
	document.attachEvent("onreadystatechange",function(){
		console.log("IE DOM ready");
	})
}else{
	document.addEventListener("DOMContentLoaded",function(){
		console.log("Webkit DOM ready");
	},false);
}