Firefox与IE在Javascript编写上的区别
1.加载事件
IE:
document.body.detachEvent("onclick",onclickfunction);document.body.attachEvent("onclick",onclickfunction);document.body.removeEventListener("click",onclickfunction, true);document.body.addEventListener("click",onclickfunction, true); btn1Obj.attachEvent("onclick",method1);btn1Obj.attachEvent("onclick",method2);btn1Obj.attachEvent("onclick",method3);btn1Obj.addEventListener("click",method1,false);btn1Obj.addEventListener("click",method2,false);btn1Obj.addEventListener("click",method3,false);input.addEventListener("click",test,false);function test(e){var e = window.event||e;var srcElement = e.srcElement || e.target;}<input onclick="test(event)">
function test() { eval("window.a = '全局变量';"); eval("window.b = function(){alert('全局函数');}"); }var isIE = document.all && window.external;function createXMLDoc(){ var xmlDoc; var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined'); var ie = (typeof window.ActiveXObject != 'undefined'); if (moz) { xmlDoc = document.implementation.createDocument("", "", null); } else if (ie) { xmlDoc = new ActiveXObject("MSXML2.DOMDocument.3.0"); xmlDoc.async = false; while (xmlDoc.readyState != 4) { }; } return xmlDoc;}function getElementValue(item){if(isIE){return item.text;}else{return item.childNodes[0].nodeValue;}}if ( ! isIE){ XMLDocument.prototype.loadXML = function(xmlString) { var childNodes = this.childNodes; for (var i = childNodes.length - 1; i >= 0; i--) this.removeChild(childNodes[i]); var dp = new DOMParser(); var newDOM = dp.parseFromString(xmlString, "text/xml"); var newElt = this.importNode(newDOM.documentElement, true); this.appendChild(newElt); }; // check for XPath implementation if( document.implementation.hasFeature("XPath", "3.0") ) { // prototying the XMLDocument XMLDocument.prototype.selectNodes = function(cXPathString, xNode) { if( !xNode ) { xNode = this; } var oNSResolver = this.createNSResolver(this.documentElement) var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) var aResult = []; aResult.length = aItems.snapshotLength; for( var i = 0; i < aItems.snapshotLength; i++) { aResult[i] = aItems.snapshotItem(i); } aResult.item=function(index){ return aResult[index] } return aResult; } // prototying the Element Element.prototype.selectNodes = function(cXPathString) { if(this.ownerDocument.selectNodes) { return this.ownerDocument.selectNodes(cXPathString, this); } else{throw "For XML Elements Only";} }// prototying the XMLDocument XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) { if( !xNode ) { xNode = this; } var xItems = this.selectNodes(cXPathString, xNode); if( xItems.length > 0 ) { return xItems[0]; } else { return null; } } // prototying the Element Element.prototype.selectSingleNode = function(cXPathString) { if(this.ownerDocument.selectSingleNode) { return this.ownerDocument.selectSingleNode(cXPathString, this); } else{throw "For XML Elements Only";} } }}<body onbeforeunload="onbeforeunload(event);"></body>
funciton onbeforeunload(e){var e = window.event||e;e..returnValue = "您是否要离开?";}$(window).bind("beforeunload",function(event){ event.originalEvent.returnValue = "您是否要离开?"; });if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){?HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)?{??switch (where){??case 'beforeBegin':???this.parentNode.insertBefore(parsedNode,this)???break;??case 'afterBegin':???this.insertBefore(parsedNode,this.firstChild);???break;??case 'beforeEnd':???this.appendChild(parsedNode);???break;??case 'afterEnd':???if (this.nextSibling) ???{????this.parentNode.insertBefore(parsedNode,this.nextSibling);???}???else ???{????this.parentNode.appendChild(parsedNode);???}???break;??}?}?HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)?{??var r = this.ownerDocument.createRange();??r.setStartBefore(this);??var parsedHTML = r.createContextualFragment(htmlStr);??this.insertAdjacentElement(where,parsedHTML)?}?HTMLElement.prototype.insertAdjacentText = function(where,txtStr)?{??var parsedText = document.createTextNode(txtStr)??this.insertAdjacentElement(where,parsedText)?}}