What is the difference between eval in Firefox and IE?

Source: Internet
Author: User

Document. getelementbyid ('globalareausercontrol1 _ ddlcontinent'). Options. selectedindex = 0

It must be said that you are good at reading program comments, but you do not know what appendchild is.

Appendchild seems to be a misunderstanding of adding a child. The result of subtree. appendchild (sub2) indicates that the DIV of sub2 is not nested in the DIV of the subtree, but after it.

Function loadcode ()
{
VaR code = getcode ();
Eval (CODE );
} // Execute getcode ()

 

It can be seen that eval cannot be executed in the global space, which brings many problems to the development, and many people have seen this depressing situation.
But now I finally found a solution. Hey hey, it can be compatible with IE and Firefox at the same time. The method is as follows:
VaR X2 ={} // my namespace :)
X2.eval = function (CODE)
{
If (!! (Window. attachevent &&! Window. Opera ))
{
// IE
ExecScript (CODE );
}
Else
{
// Not IE
Window. eval (CODE );
}
}
Now, if you want to define global code in the function, you can call the x2.eval (CODE) method. An example is as follows:
VaR S = 'global ';
Function demo3 ()
{
X2.eval ('var S = "local "');
}
Demo3 ();
Alert (s); //-> 'local'
We can see that the global variable S = "local" is redefined in the demo3 function ".
It should be noted that x2.eval does not return values. If you want to evaluate the expression, you still need to use the eval function of the system. X2.eval is designed for Global Code definition only.
In fact, some people may feel that the problem is too easy to solve, but they need some luck and skills to find this solution:
(1) For ie browsers, execScript is provided by default to execute code in the global space, but there are not many people who know it.
(2) For Firefox, if the eval function is called directly, the function is executed in the caller's space. If window. Eval is called, the function is executed in the global space. It is estimated that there will be fewer people. After all, alert (EVAL = Window. Eval) returns true!
The eval function of Firefox is indeed quite strange, but the source can also be found from the Javascript specification:
If value of the eval property is used in any way other than a direct call (that is, other than by the explicit use of its
Name as an identifier which is the memberexpression in a callexpression), or if the eval property is assigned,
An evalerror exception may be thrown.
This means that the execution of the eval function is related to the caller, but the execution context is not mentioned. Therefore, it is hard to say whether IE and Firefox are the same. It is good to know the solution.
 

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.