最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - FireFox this Function - Stack Overflow

programmeradmin1浏览0评论

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>

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>
Share Improve this question edited Mar 4, 2009 at 16:35 Chad Birch 74.6k23 gold badges154 silver badges149 bronze badges asked Mar 4, 2009 at 16:33 ericmericm 3
  • NO GUYS, it's a bug in Stackoverflow's renderer. – Jason Cohen Commented Mar 4, 2009 at 16:35
  • I've fixed the question by indenting properly. Please don't down-vote this guy just for that! – Jason Cohen Commented Mar 4, 2009 at 16:36
  • I seen that, what happened exactly? – ryeguy Commented Mar 4, 2009 at 16:36
Add a comment  | 

6 Answers 6

Reset to default 13
function drvFunc(elem) {
  alert(elem.value);
}

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

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.

Change

var d = "document."

to

var d = "document"

You are running eval with "document..frm"

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)
{

}

you could also go:

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

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

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>
发布评论

评论列表(0)

  1. 暂无评论