SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2004
    Location
    Montreal, Canada
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript + firefox + eval() problems

    I have a script that uses eval() and it works great under IE but it craps out when eval() is called in firefox. Just ignore all the commented out alerts, it is for testing purposes. When it gets to
    Code:
    y=eval("window.opener.setbgcolor."+x);
    it just stops. Why is this? Under IE it works just fine but not firefox.

    Code:
    <script language="javascript">
    function sethex(hex)
    {
    	//alert("Hex: " + hex);
    	//window.close();
    	var rgbcolor = "#" + hex;
    	rgbcolor = rgbcolor.toUpperCase();
    	var txt = "<?php echo $_GET['txt'] ?>";
    	var formnumber= "";
    	
    	var frmname = "";
    		
    	var x;
    	var y;
    	x=txt;
    	//alert("x: " + x);
    	//alert("before if");
    	//alert("addnews."+formnumber+"."+x);
    	
    	//alert("addnews."+formnumber+"."+x");
    	
    	if(frmname != "")
    	{
    		//alert("1");
    		y=eval("window.opener."+frmname+"."+x);
    		//y="document.opener."+frmname+"."+x;
    	}
    	else
    	{
    		if(formnumber=="")
    		{
    			//alert("2");
    			y=eval("window.opener.setbgcolor."+x);
    			//y="document.opener.setbgcolor."+x;
    		}	
    		else
    		{
    			//alert("3");
    			y=eval("window.opener.addnews"+formnumber+"."+x);
    			//y="document.opener.addnews"+formnumber+"."+x;
    		}
    	}
    	//alert("after if");
    	//alert("y: " + y);
    	y.value=rgbcolor;
    	y.focus();
    	alert("Getting here");
    	window.close();
    }

  2. #2
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try closing the eval-text with ;. Also, assigning the eval() to a variable is useless. It just tells you whether it executed the code or not.
    So try this:

    Code:
    eval("y = window.opener.setbgcolor." + x + ";");
    OR actually it looks like you should enclose x within parentheses (I could be wrong but it just looks like your function works that way ). Like this:

    Code:
    eval("y = window.opener.setbgcolor('" + x + "');");

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As good an illustration as any on the pitfalls of using eval() unnecessarily. It's like executing JavaScript from a parallel universe.

    Would need to see more of this...an explanation of what you're doing would help too...
    ::: certified wild guess :::


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •