您的位置:首页 > 运维架构

ie11不兼容window.createPopup的问题解决

2017-03-22 11:11 826 查看
最近在做ie11的兼容性问题,每
4000
次都会因为window.createPopup这个已经不支持,而导致会报一些稀奇古怪的错误,比如,var popup=window.createPopup之后,popup这个对象无法使用popup.document.getElementById,会报SCRIPT438: 对象不支持“getElementById”属性或方法这个错,现在终于解决的,,只要在每次调用之前加一个兼容性判断就OK,判断方法如下
if (!window.createPopup) {
var __createPopup = function() {
var SetElementStyles = function( element, styleDict ) {
var style = element.style ;
for ( var styleName in styleDict )style[ styleName ] = styleDict[ styleName ] ;
}
var eDiv = document.createElement( 'div' );
SetElementStyles( eDiv, { 'position': 'absolute', 'top': 0 + 'px', 'left': 0 + 'px', 'width': 0 + 'px', 'height': 0 + 'px', 'zIndex': 1000, 'display' : 'none', 'overflow' : 'hidden' } ) ;
eDiv.body = eDiv ;
var opened = false ;
var setOpened = function( b ) {
opened = b;
}
var getOpened = function() {
return opened ;
}
var getCoordinates = function( oElement ) {
var coordinates = {x:0,y:0} ;
while( oElement ) {
coordinates.x += oElement.offsetLeft ;
coordinates.y += oElement.offsetTop ;
oElement = oElement.offsetParent ;
}
return coordinates ;
}
return {htmlTxt : '', document : eDiv, isOpen : getOpened(), isShow : false, hide : function() { SetElementStyles( eDiv, { 'top': 0 + 'px', 'left': 0 + 'px', 'width': 0 + 'px', 'height': 0 + 'px', 'display' : 'none' } ) ; eDiv.innerHTML = '' ; this.isShow = false ; }, show : function( iX, iY, iWidth, iHeight, oElement ) { if (!getOpened()) { document.body.appendChild( eDiv ) ; setOpened( true ) ; } ; this.htmlTxt = eDiv.innerHTML ; if (this.isShow) { this.hide() ; } ; eDiv.innerHTML = this.htmlTxt ; var coordinates = getCoordinates ( oElement ) ; eDiv.style.top = ( iX + coordinates.x ) + 'px' ; eDiv.style.left = ( iY + coordinates.y ) + 'px' ; eDiv.style.width = iWidth + 'px' ; eDiv.style.height = iHeight + 'px' ; eDiv.style.display = 'block' ; this.isShow = true ; } }
}
window.createPopup = function() {
return __createPopup();
}
}

然后在接上

var winPopup = window.createPopup(); //Create the window to popup
这样就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: