您的位置:首页 > Web前端 > JavaScript

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

2014-05-07 13:18 696 查看
最近项目遇到了问题,有个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>


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐