By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

eval global declaration variable with "var " in IE

P: 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
Share this Question
Share on Google+
9 Replies


acoder
Expert Mod 15k+
P: 16,027
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

P: 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
Expert Mod 15k+
P: 16,027
You're welcome. Glad it solved your problem.
Feb 7 '07 #4

ximera
P: 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
Expert Mod 5K+
P: 5,389
@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
Expert Mod 5K+
P: 5,389
@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
P: 2
I totally agree with you, gits. It was just my observation, no more.
Jun 1 '10 #8

gits
Expert Mod 5K+
P: 5,389
of course ... it was a good and noteworthy hint ... thanks to point it out ...

kind regards
Jun 1 '10 #9

Dormilich
Expert Mod 5K+
P: 8,639
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.