473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 6364
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,658 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

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

Similar topics

4
by: raver2046 | last post by:
hello, How to have a global var in python ? thank you. raver2046 http://raver2046.ath.cx
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
9
by: lkrubner | last post by:
I've got a function, you can see it below, that is being called onmouseup in the textarea on my main form. The idea is to find a selection if possible and store that text in a global variable. I...
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
10
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
5
by: Méta-MCI | last post by:
Hi! Example, with meta-data (attributs of function) : def ff(this): try: this.count=this.count+1 except: this.count=1 a=1
4
by: thaytu888888 | last post by:
Here is my codes in aspx page: <td colspan="2" class="main_menu" runat="server" onclick='toggleDisplay(<%#Eval("description")%>);'><%#Eval("description")%></td> Here is in "View source": ...
5
by: Bruno Ferreira | last post by:
Hi, I wrote a very simple python program to generate a sorted list of lines from a squid access log file. Here is a simplified version: ################################## 1 logfile = open...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.