简体   繁体   English

IE7默认表单方法是“GET”。 如何判断它是用户输入还是默认?

[英]IE7 default form method is “GET”. How can I tell if it's user-entered or default?

If a user creates a form without a method attribute, it seems like most browsers will handle this at the time of form submission. 如果用户创建没有方法属性的表单,似乎大多数浏览器将在表单提交时处理此问题。 So upon inspection of the form element after the DOM is ready, you can see that there is no "method" attr of the form element object. 因此,在DOM准备好之后检查表单元素时,您可以看到表单元素对象没有“方法”attr。

IE7, however, apparently sets a default method value of "GET" on all forms without a method value. 但是,IE7显然在没有方法值的所有表单上设置了“GET”的默认方法值。 I don't want to argue about whether GET or POST is the most sensible default, I just want to find a way to make POST the default form method across all browsers. 我不想争论GET或POST是否是最合理的默认值,我只是想找到一种方法使POST成为所有浏览器的默认表单方法。

My problem is that I can't tell if the user entered a "GET" value for a form method, or if IE injected that value as default. 我的问题是我无法判断用户是否为表单方法输入了“GET”值,或者IE是否将该值注入默认值。 If there is no method attribute of the form, it is obvious that the users didn't specify one, so I can safely default it to POST. 如果表单没有方法属性,很明显用户没有指定一个,所以我可以安全地将其默认为POST。 But if I see a GET value for a form method, I can't tell if the user specified that, or if it was left black and IE7 set GET when it parsed the HTML. 但是如果我看到一个表单方法的GET值,我无法判断用户是否指定了它,或者它是否为黑色并且IE7在解析HTML时设置了GET。

Anyone have any ideas? 有人有主意吗?

IE's behaviour is correct !(*) According to DTD: IE的行为是正确的 !(*)根据DTD:

method      (GET|POST)     GET       -- HTTP method used to submit the form--

or, in the XHTML DTD: 或者,在XHTML DTD中:

method      (get|post)     "get"

that means if the method attribute is omitted, not only does the form submit as GET by default, but the DOM actually should contain an Attr node for method with the DTD defaulted value GET . 这意味着如果省略method属性,不仅表单默认提交为GET,而且DOM实际上应该包含具有DTD默认值GET methodAttr节点。

(*: well, sort of. IE is using the XHTML lower-case default in an HTML document where it should be the upper-case. Not that it really matters as the attribute is case-insensitive in HTML anyhow. And hey! It's IE getting the standard more-right than all the other browsers. It's a miracle!) (*:好吧,有点.IE正在HTML文档中使用XHTML小写默认值,它应该是大写的。并不是因为HTML无论如何都不区分大小写这个属性真的很重要。嘿!它是IE比其他所有浏览器都更加正确。这是一个奇迹!)

So how do you tell that the Attr node was put there because of DTD attribute defaulting and not because it was in the source? 那么你怎么知道Attr节点因为DTD属性默认而被放在那里而不是因为它在源中? With the DOM Level 1 Core specified flag: 使用DOM Level 1 Core 指定标志:

var form= document.getElementById('myform');
var attr= form.getAttributeNode('method');
var isomitted= attr===null || !attr.specified;

This doesn't seem to be in violation of the HTML form spec , which states: 这似乎没有违反HTML表单规范 ,其中规定:

This attribute specifies which HTTP method will be used to submit the form data set. 此属性指定将使用哪种HTTP方法提交表单数据集。 Possible (case-insensitive) values are "get" (the default) and "post". 可能的(不区分大小写)值是“get”(默认值)和“post”。 See the section on form submission for usage information 有关使用信息,请参阅表单提交部分

(How do I reply to a specific reply?) (in reply to bobice:) (我如何回复特定回复?)(回复bobice :)

IE's behaviour is correct! IE的行为是正确的!

If I read the relevant specs correctly, these are all the case in conformant implementations (which IE is not): 如果我正确地阅读了相关规范,那么在符合要求的实现(IE不是这样)中就是这种情况:

form.method == "get" /* IETF and W3C HTMLs and XHTMLs */ || form.method == "GET" /* HTML5* */
form.hasAttribute ("method") == false
form.getAttribute ("method") == ""
form.getAttributeNode ("method") == null

In Chrome "8.0.552.28 beta" on Linux, I get (also not correct) 在Linux上的Chrome“8.0.552.28 beta”中,我得到了(也不正确)

var form = document.createElement ("form")
undefined
form.method == "get" || form.method == "GET"
false /* actual value is "" */
form.hasAttribute ("method") == false
true
form.getAttribute ("method") == ""
false /* actual value is null */
form.getAttributeNode ("method") == null
true
  • In HTML5, method is an enumerated attribute equal to one of GET, POST, PUT, DELETE. 在HTML5中,method是枚举属性,等于GET,POST,PUT,DELETE之一。 form.method must "reflect" the method attribute, which in the case of an enumerated attribute means the specified value if it matches one of the valid values or else the first valid value. form.method必须“反映”方法属性,在枚举属性的情况下,如果它匹配其中一个有效值或第一个有效值,则表示指定值。 (I may be reading this slightly wrong, but that is my interpretation.) (我可能会读到这个有些错误,但这是我的解释。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JS中确定IE7中是否存在函数/方法? - How can I determine in JS if a function/method exists in IE7? 如何防止HTML将用户输入的文本解释为实体? - How should I prevent HTML from interpreting user-entered text as an entity? 如何更改日期输入的 innerHTML 以使该值与当前用户输入的值匹配? - How do I change the innerHTML of a date input to make the value match the currently user-entered value? 如何在IE7中获取Object元素的contentWindow - How can I get contentWindow for an Object element in IE7 如何解决JavaScript提示中用户输入的错字? - How to account for user-entered typos in a JavaScript prompt? 防止在IE7中输入Enter时默认 - prevent default on enter keypress in IE7 如何防止在IE中打开默认选项? - how can I prevent opening the default option in IE? 如何获得通过电子邮件发送到Google表单中的数据? - How can I get the data entered into a Google Form emailed to me? 使用WebKitCSSMatrix的rotationAxisAngle方法,如何有效地将以前的旋转设置为该元素的默认旋转? - Using WebKitCSSMatrix's rotateAxisAngle method, how can I effectively set the previous rotation as the default rotation for that element? 如何在iOS和Android上获取用户的默认搜索引擎? - How to get user's default search engine on iOS and Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM