javascript eval() - Invalid Argument

  • Sabu
  • Beginner
  • Beginner
  • Sabu
  • Posts: 47

Post 3+ Months Ago

Ok guys, I've looked everywhere but I'm absolutely stuck.

If I use this:
Code: [ Select ]
<script language="JavaScript">
<!--
function setBlock(block, chimage, bgimage, chlink) {
eval("top." + block + ".style.backgroundImage='" + chimage + "'");
}

setBlock("a1", "man.gif", "beach.jpg", "link");
-->
</script>
  1. <script language="JavaScript">
  2. <!--
  3. function setBlock(block, chimage, bgimage, chlink) {
  4. eval("top." + block + ".style.backgroundImage='" + chimage + "'");
  5. }
  6. setBlock("a1", "man.gif", "beach.jpg", "link");
  7. -->
  8. </script>

it gives me 'invalid argument' for the eval function. However, if I remove the ' ' around chimage, like so

Code: [ Select ]
<script language="JavaScript">
<!--
function setBlock(block, chimage, bgimage, chlink) {
eval("top." + block + ".style.backgroundImage=" + chimage);
}

setBlock("a1", "man.gif", "beach.jpg", "link");
-->
</script>
  1. <script language="JavaScript">
  2. <!--
  3. function setBlock(block, chimage, bgimage, chlink) {
  4. eval("top." + block + ".style.backgroundImage=" + chimage);
  5. }
  6. setBlock("a1", "man.gif", "beach.jpg", "link");
  7. -->
  8. </script>


It gives me "'man' is undefined".

How can I write this to put the string chimage as the backgroundimage without messing up the eval function?

Extra notes:
This code is within an iframe, and the block 'a1' is a div outside of the iframe.
bgimage and chlink will be used later, but are not used for the purpose of this specific question. Obviously, once I do use them I'll use the same hints I get here
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

You just need to add url() for it to work in IE:

Code: [ Select ]
function setBlock(block, chimage, bgimage, chlink) {
eval("top." + block + ".style.backgroundImage='url(" + chimage + ")'");
}
  1. function setBlock(block, chimage, bgimage, chlink) {
  2. eval("top." + block + ".style.backgroundImage='url(" + chimage + ")'");
  3. }


For it to work in Mozilla/Firefox as well as IE I think you would need this:

Code: [ Select ]
function setBlock(block, chimage, bgimage, chlink) {
eval("top.document.getElementById('" + block + "').style.backgroundImage='url(" + chimage + ")'");
}
  1. function setBlock(block, chimage, bgimage, chlink) {
  2. eval("top.document.getElementById('" + block + "').style.backgroundImage='url(" + chimage + ")'");
  3. }


It might be easier to do it without the eval though:

Code: [ Select ]
function setBlock(block, chimage, bgimage, chlink) {
    var myDiv = top.document.getElementById(block);
    myDiv.style.backgroundImage='url(' + chimage + ')';
}
  1. function setBlock(block, chimage, bgimage, chlink) {
  2.     var myDiv = top.document.getElementById(block);
  3.     myDiv.style.backgroundImage='url(' + chimage + ')';
  4. }


Then you could reuse the myDiv reference without having to debug the evals.
  • Sabu
  • Beginner
  • Beginner
  • Sabu
  • Posts: 47

Post 3+ Months Ago

Double cool, working fantastically

thanks!

After experimenting for a good half an hour, I can't see how I was all that much different, but suddenly your second example works fine :crazyeyes:

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 105 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.