Compatibility issues and solutions for window.exe cscript

Source: Internet
Author: User

The first thing I know is how to use the intellij idea function. Later, I want to know that there is a window.exe cScript in ie. In fact, these two functions are quite different.

Let's give an example.

Demo1:

Var globalV = 123;
Function testEval (){
Eval ("var globalV = 'global '");
}
TestEval ();
Alert (globalV); // 123 is displayed, which is true for both ie and ff.

Demo2:

Var globalV = 123;
Function testEval (){
ExecScript ("var globalV = 'global '");
}
TestEval ();
Alert (globalV); // IE pops up global, ff does not have the execScript Method

So how can we solve the problem of execScript compatibility. Today, I read the source code of mootools and found a better solution.

Var exec = function (text ){
If (! Text) return text;
If (window.exe cScript ){
Window.exe cScript (text );
} Else {
Var script = document. createElement ('script ');
Script. setAttribute ('type', 'text/javascript ');
Script. text = text;
Document. head. appendChild (script );
Document. head. removeChild (script );
}
Return text;
}

It means to dynamically insert and delete a piece of js Code, so that the code can be executed in the global space. This method is also seen in the Jquery source code before. The implementation principle is the same.

// Evalulates a script in a global context
GlobalEval: function (data ){
If (data & rnotwhite. test (data )){
// Specified red by code by Andrea Giammarchi
// Http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
Var head = document. head | document. getElementsByTagName ("head") [0] | document.doc umentElement,
Script = document. createElement ("script ");

If (jQuery. support. scriptEval ()){
Script. appendChild (document. createTextNode (data ));
} Else {
Script. text = data;
}

// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709 ).
Head. insertBefore (script, head. firstChild );
Head. removeChild (script );
}
},

I also thought of a simple method.

Var exec = function (text ){

If(window.exe cScript ){

Window.exe cScript (text );

Return text;

}

Window. eval. call (window, text); // call is used to change the eval execution environment, but this method is invalid in ie. I don't know why

Return text;

}

It turns out that eval and window. eval are different.

Var exec=window.exe cScript | function (statement ){

// If the browser is normal, use window. eval
Return window. eval (statement );
},

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.