views:

427

answers:

6

Why does Firefox not handle this. This code works in IE.

<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<script type='text/javascript'>
function drvFunc(elem)
{
    var e = elem.name;
    var d = "document."
    var f = "frm";
    var str = d+"."+f+"."+e+".value;";
    alert(eval(str));
}
</script>
<BODY>
<form name=frm method=post>
<input type=button name=myButton id=myButton value='MyButton' onclick='drvFunc(this)'>
</form>
</BODY>
</HTML>
+8  A: 

The problem is that you have two periods being concatenated:

  1. var d = "document."
  2. var str = d+"."+f...

Your resulting string becomes: "document..frm.myButton.value;"

Remove one of the periods and it will work.

Cerebrus
Or even better, use:alert(document.frm[elem.name].value);And get rid of everything else in the function;.
Rakesh Pai
I agree... but this appears to be a learning exercise for the OP so I did not suggest (better) alternatives. ;-)
Cerebrus
+12  A: 
function drvFunc(elem) {
  alert(elem.value);
}

You don't need evil eval() for this function ...

f3lix
I would like to give you +10 votes for than answer.
some
+1  A: 

Change

var d = "document."

to

var d = "document"

You are running eval with "document..frm"

Ascalonian
+1  A: 

I'm the original author of this code thread. I'm not sure I stated the problem correctly.

function drvFunc(elem)
{
    **var e = elem.name;** <-- in firefox, this fails.  e is not initialized!! 
    var d = "document."
...
}

On a form I might write some code like this and it works fine in IE...

<input type=button name=1stButton id=1stButton onclick='drvFunc(this)'>
<input type=button name=2ndButton id=2ndButton onclick='drvFunc(this)'>

... then drvFunc could do this

function drvFunc(elem)
{

}
quote attribute values!
SilentGhost
and put script in some more appropriate place, it cannot be a child of a document
SilentGhost
To the original author: prefix all code with 4 spaces (mark the code and then click on the 0101 button) and it will be displayed correctly. If you look below your edit window you have a preview of how it will look. I edited this post for you.
some
e is initialised, btw
SilentGhost
A: 

You need to put quotes (single or double) around the attributes on your <input> tag.

Firefox is probably handling unquoted attibutes differently to IE: http://www.cs.tut.fi/~jkorpela/qattr.html

You also need to remove the extra dot in "document." as others have said, and should probably refactor the drvFunc to remove eval as well.

The following works perfectly for me under Firefox 3:

<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<script type='text/javascript'>
function drvFunc(elem)
{
    alert(elem.value);
}
</script>
</HEAD>
<BODY>
<form name="frm" method="post">
<input type="button" name="myButton" id="myButton" value="MyButton" onclick="drvFunc(this)">
</form>
</BODY>
</HTML>
Mark Pim
+1  A: 

you could also go:

<input type='button' name='2ndButton' id='2ndButton' onclick='drvFunc(this.id)'> 

function drvFunc(elemid){ 
   alert(document.getElementById(elemid).value); 
}
mike nvck