JavaScript overwrites the document. createElement Method to Solve the Problem of incompatibility between window. close and Firefox)

Source: Internet
Author: User

Recently encountered a problem in the project, there is a asp.net web program can only run in IE7, now xp are eliminated, everyone uses IE8-IE11, so this web app also needs to upgrade to adapt to all IE versions. According to the incompatibility of IE, the document. createElement method is called, for example:

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 (" ");
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 );

}

There are too many js files in this system, and you are not familiar with the business of this system. I used to replace this document. createElement with jquery,

Var imgDel = jq (" ") [0];

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

Later I found that there were too many places to change. So think about whether there is a simple method, and finally point the finger to overwrite the implementation of the document. createElement method.

Document. createEl = document. createElement;
Document. createElement = function (obj ){
If (obj. toString (). indexOf ("<")>-1 ){
Return jq (obj) [0];
}
Else {
Return document. createEl (obj );
}
}

No exceptions have been found in ie.

All those familiar with the front-end know that Firefox's non-window. open page window. close by default is invalid.

Many people on the Internet say that they enter "about: config" in the address bar of Firefox and find dom. allow_scripts_to_close_windows; Change "false" to "true ".

Looking at these people's opinions, I have to say that I am suffering.

I am working on a website. How can I modify the user's browser settings? I am not a virus.

Do I publish an announcement on my website: "If you need Firefox to access this website, please modify the browser settings ......"

I'm afraid I will die very quickly.

It is impossible to close it, so let's make a compromise .. Jump to 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>


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.