JavaScript 覆蓋document.createElement 方法 解決window.close在Firefox下不相容問題)

來源:互聯網
上載者:User

最近項目遇到了問題,有個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下還沒有發現什麼異常情況。

熟悉前端的都知道,Firefox預設狀態非window.open的頁面window.close是無效的

網上有很多人說,在Firefox的地址欄輸入:about:config然後找到dom.allow_scripts_to_close_windows;把false改為true

看著這些人的說法,不得不說我蛋疼了

我做的是網站,我怎麼去改使用者的瀏覽器設定,我不是搞病毒的啊

難道我在網站發布一個公告“如需用Firefox訪問本網站,請修改瀏覽器器設定……”

那恐怕我會死得很快

關閉是不可能的,那就搞點折中方案。。跳轉到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>


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.