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 );
},