469,342 Members | 5,593 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

eval global declaration variable with "var " in IE

2
Hi,

I am a new member of this forum but I'm french so... make easy sentences which I can understand please :)

I have a javascript code in a string variable :
var jscode = "var test = 'test variable';";

I want to declare the variable "test" in the global context for I can take the value from anywhere. But I can declare it only from a function.

this code run very well in FireFox and Opera :
Expand|Select|Wrap|Line Numbers
  1. function evalcode(){
  2.   var jscode = "var test = 'test variable';";
  3.   window.eval(jscode);
  4. }
  5. evalcode();
  6. alert(test); // "test variable"
but in Internet Explorer, does not work... the variable "test" is not declare in the global context :
Expand|Select|Wrap|Line Numbers
  1. function evalcode(){
  2.   var jscode = "var test = 'test variable';";
  3.   window.eval(jscode);
  4.   alert(test); // "test variable"
  5. }
  6. evalcode();
  7. alert(test); // error
If I remove the key word "var " before my variable "test", all work good.
But the majority of javascript framework declare their variables with this key word and I would like to be able to charge them without modify them.

somebody have an idea?
Feb 7 '07 #1
9 6122
acoder
16,027 Expert Mod 8TB
Welcome to The Scripts, xorax.

In IE, you can use the proprietary window.execScript. It doesn't return anything unlike eval:
Expand|Select|Wrap|Line Numbers
  1. if (window.execScript) {
  2. window.execScript(code);
  3. return null;
  4. } else { // other browsers
You must know that eval is considered evil! You must avoid its use as much as possible, but in this particular case, where you need on-demand Javascript (I assume), you might have no choice.

One other option for Firefox and other browsers is to create a reference to the global scope and call eval on that, but I guess you do that using window.eval.
Feb 7 '07 #2
xorax
2
thanks you !!!
that's working very well !

Yes I call javascript on-demand after XMLHttpRequest and I have no other choice.

The method window.eval is working for all browsers at exeption of IE I think but, in related discusions, it appears that Safari don't accept window.eval (and naturally window.execScript because specific to IE).

The method used for Safari is :
window.setTimeout( code, 0 );

But I don't have Mac so I can't test this...

Safari is not very used but, I think, in expanding in the next years.
If somebody have Safari for test this, thank you with him to present its results.

thank you again acoder.
Feb 7 '07 #3
acoder
16,027 Expert Mod 8TB
You're welcome. Glad it solved your problem.
Feb 7 '07 #4
ximera
2
first of all sorry for my english

there is some more problem in ie with eval
if you write
var question=eval('({"quien" : "pedro", "donde" : "alla", "cuando" : "hoy" })');
it is normal
but if you write
var question=eval('({"quien" : "pedro", "donde" : "alla", "cuando" : "hoy", })'); (WITH COMMA in fin)
appear problem in ie that doesn't appear in other browsers
SO, you need to avoid last COMMA, it is important in ie and cause troubles

good luck :)
Jun 1 '10 #5
gits
5,390 Expert Mod 4TB
@xorax
i suspect that IE dosn't apply the context - so the following might work correctly in all browsers:

Expand|Select|Wrap|Line Numbers
  1. function evalcode(){
  2.   var jscode = "var test = 'test variable';";
  3.   eval.call(window, jscode);
  4. }
  5. evalcode();
  6.  
  7. alert(test);
Jun 1 '10 #6
gits
5,390 Expert Mod 4TB
@ximera
basically i have (unfortunately) to admit that from my point of view IE does it right this time ... a trailing comma just implies an empty element/property at the end ... so i don't understand why other JavaScript engines just ignore that 'error' -> so basically i would suggest to always avoid trailing commas in arrays and object-declarations ... since it is syntactically more correct ... and for example: just saying that in PHP or another language the trailing comma is allowed is no reason that it would be really correct ... :)

so this case is one of those very rare ones where i would say that IE is right in complaining about something :)
Jun 1 '10 #7
ximera
2
I totally agree with you, gits. It was just my observation, no more.
Jun 1 '10 #8
gits
5,390 Expert Mod 4TB
of course ... it was a good and noteworthy hint ... thanks to point it out ...

kind regards
Jun 1 '10 #9
Dormilich
8,651 Expert Mod 8TB
so basically i would suggest to always avoid trailing commas in arrays and object-declarations
while that is true, a trailing comma in a function call always implies the last parameter being undefined, which is a valid value for arrays (due to the unlimited amount (ok, less than 4 million something) of passed parameters).
Jun 2 '10 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by raver2046 | last post: by
4 posts views Thread by Andrew V. Romero | last post: by
9 posts views Thread by lkrubner | last post: by
10 posts views Thread by Kobu | last post: by
5 posts views Thread by Bruno Ferreira | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.