WoW Gold - Debt Consolidation - Wordpress Themes - Premium wordpress themes - Find jobs

PDA

View Full Version : JS 'eval' function issue


dorinp
Jun 28th 2008, 9:40 am
Hi,
Have you deal with this 'eval' function issue ?
In the JS code below - option a) work well but the b) is not working.
Have you any idea why? It's this a JS bug ?
I need this for having a line like: eval('document.all.counter_' + param1 + '.innerHTML') = 123;
Thanks,
Dorin

//--------JS script----------
function test()
{
html_code = '<div id="mydiv"></div>';
document.write(html_code);

// -- a)
document.all.mydiv.innerHTML = 123;
// -- b)
eval('document.all.mydiv.innerHTML') = 123;
}

koolman
Jun 28th 2008, 10:59 am
you may try:

myObj = eval('document.all.counter_' + param1);
myObj.innerHTML = 123;

Nafai
Jun 29th 2008, 2:28 pm
eval('document.all.mydiv.innerHTML') = 123;

This doesn't work because document.all.mydiv.innerHTML is a pointer to the innerHTML property of the mydiv object. When you call eval('document.all.mydiv.innerHTML'), you get back the CONTENTS of the mydiv, returned as a string. The value returned is literally the string 'whatever html here'. When you try to set THAT STRING equal to something else, you are trying to change the string only, at that point it has no idea that it WAS the innerHTML of a div, all it knows is it is a string.

But you can do it like this:
eval('document.all.mydiv').innerHTML = 123;