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

javascript - Reading form action property in IE6, if form has a field named "action" - Stack Overflow

programmeradmin5浏览0评论

Given the form below:

<form id="myForm" action="index.php">
    <input type="hidden" name="action" value="list" />
    <input type="submit" />
</form>

How can I get the value for the action property of the form (index.php) using IE6?

Hint: I have tried

document.getElementById('myForm').action

and

document.getElementById('myForm').getAttribute('action')

But neither of them work. They both return the input field (document.getElementById('myForm').action.value == 'list').

Given the form below:

<form id="myForm" action="index.php">
    <input type="hidden" name="action" value="list" />
    <input type="submit" />
</form>

How can I get the value for the action property of the form (index.php) using IE6?

Hint: I have tried

document.getElementById('myForm').action

and

document.getElementById('myForm').getAttribute('action')

But neither of them work. They both return the input field (document.getElementById('myForm').action.value == 'list').

Share Improve this question asked Sep 4, 2009 at 9:26 TomTom 7,25315 gold badges64 silver badges79 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

There is a simple way, using the node's attributes collection:

document.getElementById("myForm").attributes["action"].value

Test page to try it out (also demonstrates the brokenness of getAttribute as mentioned David Dorward's answer):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Form Action Access</title>
<script type="text/javascript">
function dotAccess()
{
    alert(document.getElementById("myForm").action.value);
}
function getAttributeAccess()
{
    alert(document.getElementById("myForm").getAttribute("action"));
}
function attributesAccess()
{
    alert(document.getElementById("myForm").attributes["action"].value);
}
</script>
</head>
<body>
<form id="myForm" action="foo">
  <input type="hidden" name="action" value="bar">
  <input type="button" onclick="dotAccess()" value="form.action">
  <input type="button" onclick="getAttributeAccess()" value='form.getAttribute("action")'>
  <input type="button" onclick="attributesAccess()" value='form.attributes["action"]'>
</form>
</body>
</html>

David's right. Here's a less nasty way than the regex though:

var action= document.forms.myForm.cloneNode(false).action;

(Because the clone is shallow, there is no ‘input name="action"’ inside the new form element to confuse it.)

There isn't a simple way.

The presence of the action field, clobbers the action property of the form and getAttribute is broken in IE < 8.

The only way I can think of is to get dirty with regular expressions. I would very strongly suggest changing the form to use a different name for the field than 'action' if at all possible instead.

var form = document.forms.myForm;
var action = form.getAttribute('action');
if (action === form.elements.action) {
       // getAttribute broken;
       var string = form.outerHTML;
       return string.match(/action=(.*?)[> ]/)[1];
} else {
       return action;
}

That regular expression might not be robust enough. So test it hard if you do use it.

I've just found another way: using or cloning the readAttribute method available in prototypejs.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论