对 HTML 表单的 action 属性使用空 URL 是一种好习惯吗?(动作=“")
我想知道是否有人可以对使用空白 HTML 表单操作回发到当前页面给出最佳实践"响应.
I am wondering if anyone can give a "best practices" response to using blank HTML form actions to post back to the current page.
有一个帖子询问空白 HTML 表单操作在这里做什么和一些页面如这个 建议它很好,但我想知道人们的想法.
There is a post asking what a blank HTML form action does here and some pages like this one suggest it is fine but I'd like to know what people think.
推荐答案
你能做的最好的事情就是完全省略 action 属性.如果您不填写,表单将被提交到文档的地址,即同一页.
The best thing you can do is leave out the action attribute altogether. If you leave it out, the form will be submitted to the document's address, i.e. the same page.
也可以将其留空,任何实现 HTML 的表单提交算法 会将其视为等同于文档的地址,它这样做主要是因为浏览器目前是这样工作的:
It is also possible to leave it empty, and any browser implementing HTML's form submission algorithm will treat it as equivalent to the document's address, which it does mainly because that's how browsers currently work:
8.
让 action 成为提交者元素的 动作.
8.
Let action be the submitter element's action.
9.
如果 action 是空字符串,则让 action 为 文档地址.
9.
If action is the empty string, let action be the document's address.
注意:这一步是 故意违反 RFC 3986,这将需要在此处进行基本 URL 处理.这种违规的动机是希望与遗留内容兼容.[RFC3986]
Note: This step is a willful violation of RFC 3986, which would require base URL processing here. This violation is motivated by a desire for compatibility with legacy content. [RFC3986]
这绝对适用于所有当前浏览器,但在某些旧浏览器中可能无法正常工作("浏览器用空的 action="" 属性") 做奇怪的事情,这就是为什么 规范强烈劝阻作者不要离开它空:
This definitely works in all current browsers, but may not work as expected in some older browsers ("browsers do weird things with an empty action="" attribute"), which is why the spec strongly discourages authors from leaving it empty:
action
和 formaction
内容属性(如果指定)必须具有 有效的非空 URL 可能被空格包围.
The
action
andformaction
content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces.
相关文章