JavaScript 覆盖document.createElement 方法 解决window.close在火狐下不兼容问题)

时间:2023-03-09 03:33:08
JavaScript 覆盖document.createElement 方法 解决window.close在火狐下不兼容问题)

近期项目遇到了问题,有个asp.net web程序仅仅能在IE7 执行。如今xp都淘汰了,大家都用IE8-IE11,因此这个web app也须要升级 适应全部IE版本号。照成IE版本号不兼容的问题主要来致document.createElement方法的调用,如:

function addStyleNo(value, cannotDel) {

    if (!value) {

        value = '';

    }

    var tb = $('tbodyStyle');

    var tr = tb.insertRow();

    var td1 = tr.insertCell();

    td1.style.width = '20px';

    td1.style.height = '20px';

    if (!cannotDel) {

        var imgDel = document.createElement("<img alt = '' src='./images/delete.gif' onclick = 'delScTr(this)' style='cursor:pointer' />");

        td1.appendChild(imgDel);

    }



    var td2 = tr.insertCell();

    td2.style.height = '20px';

    var txt = document.createElement("<input type = 'text' class = 'ip-bx-ro' value = '" + value + "' />");

     td2.appendChild(txt);



}

这个系统的js太多太多,大家对这个系统的业务也不熟悉。我先前是把这个document.createElement 用jquery来取代。

var imgDel = jq("<img alt = '' src='./images/delete.gif' onclick = 'delScTr(this)' style='cursor:pointer' />")[0];

var txt = jq("<input type = 'text' class = 'ip-bx-ro' value = '" + value + "' />")[0];

后来发现要改的地方太多了。于是想想有没有简单的方法, 最后把矛头指向覆盖document.createElement 方法的实现。

document.createEl = document.createElement;

        document.createElement = function (obj) {

            if (obj.toString().indexOf("<") > -1) {

                return jq(obj)[0];

            }

            else {

                return document.createEl(obj);

            }

        }

眼下在ie下还没有发现什么异常情况。

熟悉前端的都知道,火狐默认状态非window.open的页面window.close是无效的

网上有非常多人说,在火狐的地址栏输入:about:config然后找到dom.allow_scripts_to_close_windows;把false改为true

看着这些人的说法,不得不说我蛋疼了

我做的是站点。我怎么去改用户的浏览器设置。我不是搞病毒的啊

难道我在站点公布一个公告“如需用火狐訪问本站点,请改动浏览器器设置……”

那恐怕我会死得非常快

关闭是不可能的。那就搞点折中方案。。跳转到about:blank嘛

 <script type="text/javascript">
function CloseWebPage() {
if (navigator.userAgent.indexOf("MSIE") > 0) {
if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
window.opener = null; window.close();
}
else {
window.open('', '_top'); window.top.close();
}
}
else if (navigator.userAgent.indexOf("Firefox") > 0) {
window.location.href = 'about:blank ';
//window.history.go(-2);
}
else {
window.opener = null;
window.open('', '_self', '');
window.close();
}
}
</script>