A permanent solution to button failure in earlier versions of ewebeditor in new ie versions

Source: Internet
Author: User

There is a set of news publishing systems in the Organization. It was a long time ago and has been in use. The version of ewebeditor is also unclear, but it must be an old version.

A problem occurred some time ago, and the buttons on IE8 failed. After Baidu, almost all solutions were the same. They all judged the IE version and then executed the corresponding anonymous method.

If (element. yuseronclick) eval (element. yuseronclick + "anonymous ()");

There are two solutions without exception.

1,

1  if(navigator.appVersion.match(/MSIE (7|8)\./i)!=null || navigator.appVersion.match(/MAXTHON/i)=='MAXTHON')2 {3     if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "onclick(event)");4 }else{5     if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous()");6 }

This solution uses the version check method to call different function names for different ie versions. However, if later versions of IE are popular, the version number must be added to determine the solution, if IE modifies the function name, it is more difficult to judge.

2,

if (element.YUSERONCLICK){try{eval_r(element.YUSERONCLICK + "onclick(event)");}catch (e){eval_r(element.YUSERONCLICK + "anonymous()");}}
This method does not detect the IE version number, but uses try/catch to distinguish the version number. Similarly, when ie changes the function name, a try/catch layer is nested.

3. This is the solution I designed.

It seems that only these two solutions are available, and it seems that they have been switched to standard solutions. In my opinion, the code of these two methods is too much, and it cannot cope with the problem that the function name may be changed again after ieupgrade. After carefully analyzing the problematic code, you will find that the element. yuseronclick attribute stores a function definition. Alert will see the following code string:

Function anonymous () {// here is the Execution Code}

To put it completely, Eval is used to execute a js code snippet as follows:

Function anonymous () {// here is the Execution Code} anonymous ()

Due to the upgrade of IE, the anonymous () function name changes. However, when we use the function name as a constant string in the code, this change cannot be adapted.

Of course, we can also use the character extraction method to split the function name from the code segment, and then piece together the code segment for execution. Although the method is okay, it is not concise enough.

In fact, using the dynamic characteristics of JS, we can directly and correctly execute this function without having to know the function name or dynamically extract the function name.

The final code will save Code bytes more than the source code.Replace the function name with a pair of parenthesesYou can solve the problem:

if (element.YUSERONCLICK) eval("(" + element.YUSERONCLICK + ")()");

In this way, not only does the Code not be added, but the weight is reduced, andNo matter what the function name becomes after IE is upgraded, As long as IE does not cancel this function.

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.