473,385 Members | 1,402 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,385 software developers and data experts.

javascript dereference?

Hi,

I have the following code:

function abc(){
eval('function __newfunc(){alert("hello");}');
}
abc();

now outside of that script context I try to call __newfunc(); and it
fails.
In firefox and opera all I had to do is change eval to window.eval to
force it to
execute in the window context. Unfortunetly that does not work with IE.
However
I could just do the following: window.__newfunc = __newfunc. and force
it to register.
My question is how can I do that without knowing the function name of
the one being
defined inside the eval expression, in this case __newfunc().

Thanks

Aug 5 '06 #1
5 5319
a.*******@gmail.com writes:
function abc(){
eval('function __newfunc(){alert("hello");}');
Which could just as well be:
function __newfunc(){alert("hello");}
}
abc();

now outside of that script context I try to call __newfunc(); and it
fails.
As it should.
In firefox and opera all I had to do is change eval to window.eval
to force it to execute in the window context. Unfortunetly that does
not work with IE.
It's not guaranteed to work like that, or at all. It's a proprietary
feature of those browsers that is not matched by other browsers. That
happens all the time, usually in the opposite direction :)
However I could just do the following: window.__newfunc =
__newfunc. and force it to register.
That's just assigning to a property of the global object, which makes
the assigned value available as a global variable.
My question is how can I do that without knowing the function name
of the one being defined inside the eval expression, in this case
__newfunc().
First of all, why do you want to use eval? Is the function declaration
provided by the user or in some other way not known when the abc
function was written?

Assuming the function comes as a string (bad choice, but it happens):

function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
}

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 5 '06 #2


Lasse Reichstein Nielsen wrote:

function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
^^^^^^
MS JScript does not expose a name property on function objects. Nor does
the script engine used in Opera 8 or 9.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 5 '06 #3
Lasse Reichstein Nielsen wrote:
a.*******@gmail.com writes:
function abc(){
eval('function __newfunc(){alert("hello");}');

Which could just as well be:
function __newfunc(){alert("hello");}
}
abc();

now outside of that script context I try to call __newfunc(); and it
fails.

As it should.
In firefox and opera all I had to do is change eval to window.eval
to force it to execute in the window context. Unfortunetly that does
not work with IE.

It's not guaranteed to work like that, or at all. It's a proprietary
feature of those browsers that is not matched by other browsers. That
happens all the time, usually in the opposite direction :)
However I could just do the following: window.__newfunc =
__newfunc. and force it to register.

That's just assigning to a property of the global object, which makes
the assigned value available as a global variable.
My question is how can I do that without knowing the function name
of the one being defined inside the eval expression, in this case
__newfunc().

First of all, why do you want to use eval? Is the function declaration
provided by the user or in some other way not known when the abc
function was written?

Assuming the function comes as a string (bad choice, but it happens):

function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
}
Unfortunetly none of the two is the case. Infact I dont know what the
javascript code would be. It may define from 1-n number of functions.
Also I cant know before hand what the function name would be.
I am trying to find a way to execute the javascript code without having
to do something like:
document.write('<script language="javascript"code snip here
</script>');

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 5 '06 #4
Martin Honnen <ma*******@yahoo.dewrites:
Lasse Reichstein Nielsen wrote:

> function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
^^^^^^
MS JScript does not expose a name property on function objects. Nor
does the script engine used in Opera 8 or 9.
True. And I thought I checked in Opera, but it must have been Firefox.

Well, in that case, one will have to pick the name out of the string:

function abc(funcString) {
var f = eval("("+funcString+")");
var nameMatch = /function\s+([\w$]+)\s*\(/.exec(funcString);
if (nameMatch) {
window[nameMatch[1]] = f;
}
}

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 5 '06 #5
JRS: In article <44***********************@newsread4.arcor-online.net>,
dated Sat, 5 Aug 2006 14:02:14 remote, seen in
news:comp.lang.javascript, Martin Honnen <ma*******@yahoo.deposted :
>

Lasse Reichstein Nielsen wrote:

> function abc(funcString) {
var f = eval("("+funcString+")");
window[f.name] = f;
^^^^^^
MS JScript does not expose a name property on function objects. Nor does
the script engine used in Opera 8 or 9.
Let the function be F : will the second word in F.toString() always be
F, the 'name' of the function?
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matthias Kaeppler | last post by:
Hello, I need to sort a range of pointers with a predicate which applies to the pointees. I tried to use boost::indirect_iterator, however, this will sort the container with the pointees instead...
1
by: jarit | last post by:
Hi, Found these coding guidelines for C#, HTML, Javascript, Java, HTML, PL/SQL, T-SQL, VB and VBScript. Well written and free to download. www.demachina.com/products/swat Jeroen
5
by: Kurt Lange | last post by:
How to I dereference a pointer to an object. I say dereference, because I don't now how to better explain this.... function firstFunction(byval obj as class1) as class1 dim returnVal as...
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
1
by: devvan | last post by:
I need to get information about certain fields in an HTML tag. "Field" may be the incorrect term, but what I mean is the information stored in "thing" or "widgetId" in the HTML tag below. <div...
30
by: somenath | last post by:
Hi All, I have one doubt regarding pointer .I have one small code as mentioned bellow . #include<stdio.h> #include<stdlib.h> int main (void) { int *p; int *ptr= malloc(sizeof(int *));
10
by: Summercool | last post by:
so many places, including the book PHP in a Nutshell, p. 80, it says: $a =& $b # set $a to reference $b if $a reference $b, then while you can say $b =1, you can't really say $a = 1. you...
17
by: Tony Jackson | last post by:
Hi I'm quite new to C programming - I have more of a Java background: maybe someone here can advise me. I'm nearly finishing a little program but it has some hard-to-find bugs that basically...
26
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Do you think we can reach any kind of consensus on whether the following code's behaviour is undefined by the Standard? int my_array; int const *const pend = *(&my_array + 1); Considering...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.