On IE when Eval encounters function processing _javascript skills

Source: Internet
Author: User
There is no function in the 1:eval, direct execution:
Eval ("Alert (' SS ');"); /all browsers are output correctly
The situation in 2:eval has function,function immediate execution:
Eval (function () {alert (' SS ');}) ();");/ /all browsers correct output
There is a function in the 3:eval, a function reference is saved using a variable, and the function is invoked:
var f=eval ("(function () {alert (' SS ');}");
f ();//ie error: Missing object other browser normal
When the eval defines a function and returns it to the variable, ie complains: missing object. It can be seen that the function defined in the eval under IE does not return to the eval outside successfully.

WORKAROUND: Make the function object return as a result of execution:

Method 1:

var f=eval (() (function () {return function () {alert (' SS ');}}) ()");
f ();//all browsers correct output
Eval calls an immediately executing function that returns a function object after it executes, at which point the reference to that function object is successfully returned to the external variable.

Method 2:

var f=eval ("(false| | function () {alert (' SS ');});
f ();//all browsers successfully output
This method is also the method used in jquery, function as the result of the execution of the expression or return, the same can successfully solve the problem. Of course, the expression is not limited to the above false| | function () {}, which resolves the problem if a function is returned successfully by a variety of expressions:

/* and expression: */
var f=eval ("(True&&function () {alert (' SS ');}");
f ();//all browsers normal output

/* Ternary expression: * *
var f=eval ("(True?function () {alert (' SS ');}: ');");
f ();//all browsers normal output

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.