对 HTML 表单的 action 属性使用空 URL 是否是一种好习惯?(动作=“") [英] Is it a good practice to use an empty URL for a HTML form's action attribute? (action="")

查看:33
本文介绍了对 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:

actionformaction 内容属性(如果指定)必须具有 可能被空格包围的有效非空 URL.

The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces.

这篇关于对 HTML 表单的 action 属性使用空 URL 是否是一种好习惯?(动作=“")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆