我想知道是否有人可以给出一个“最佳实践”的回应,使用空白HTML表单动作发布回当前页面。

有一个帖子问一个空白的HTML表单动作在这里做什么,一些像这样的页面认为它是好的,但我想知道人们是怎么想的。


当前回答

I use to do not specify action attribute at all. It is actually how my framework is designed all pages get submitted back exact to same address. But today I discovered problem. Sometimes I borrow action attribute value to make some background call (I guess some people name them AJAX). So I found that IE keeps action attribute value as empty if action attribute wasn't specified. It is a bit odd in my understanding, since if no action attribute specified, the JavaScript counterpart has to be at least undefined. Anyway, my point is before you choose best practice you need to understand more context, like will you use the attribute in JavaScript or not.

其他回答

实际上,当前HTML5草案的Form Submission分段不允许action=""。这是违反规格的。

action和formaction内容属性(如果指定)必须具有一个有效的非空URL值,该值可能被空格包围。(重点)

墨卡托的回答中引用的部分是对实现的要求,而不是对作者的要求。作者必须遵循作者要求。如何阅读本规范:

特别是,有一些一致性要求适用于生产者,例如作者和他们创建的文档,还有一些一致性要求适用于消费者,例如Web浏览器。它们可以通过它们的需求来区分:对生产者的需求说明了允许什么,而对消费者的需求说明了软件如何操作。

html4允许一个空的url,而html4的改变是因为“浏览器对一个空的action=”“属性”做了一些奇怪的事情。考虑到更改的原因,最好也不要在HTML4中这样做。

在HTML 5中action=""是不支持的,所以不要这样做。不好的实践。

如果你完全否定动作,默认情况下它会提交到同一个页面,我相信这是最好的做法:

<form>This will submit to the current page</form>

如果您使用php提交表单,您可能需要考虑以下内容。点击这里阅读更多信息。

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

或者你也可以使用# bear in mind,这将像一个锚,滚动到页面顶部。

<form action="#">

当你放空动作时,一些安全过滤认为它是恶意的或网络钓鱼。因此,他们可以阻止你的页面。所以建议不要让action=为空。

I use to do not specify action attribute at all. It is actually how my framework is designed all pages get submitted back exact to same address. But today I discovered problem. Sometimes I borrow action attribute value to make some background call (I guess some people name them AJAX). So I found that IE keeps action attribute value as empty if action attribute wasn't specified. It is a bit odd in my understanding, since if no action attribute specified, the JavaScript counterpart has to be at least undefined. Anyway, my point is before you choose best practice you need to understand more context, like will you use the attribute in JavaScript or not.

您能做的最好的事情就是完全省略action属性。如果你省略了它,表格将被提交到文件的地址,即同一页。

也可以将其保留为空,任何实现HTML表单提交算法的浏览器都会将其视为等同于文档的地址,这主要是因为这是浏览器当前的工作方式:

8. 让action成为提交者元素的动作。 9. 如果action为空字符串,则让action为文档的地址。 注意:这一步是故意违反RFC 3986的,它需要在这里处理基本URL。这种违反的动机是希望与遗留内容兼容。(RFC3986)

这肯定适用于当前所有的浏览器,但在一些旧的浏览器中可能无法正常工作(“浏览器会对空的action=""属性"做一些奇怪的事情),这就是为什么规范强烈不鼓励作者让它为空:

action和formaction内容属性(如果指定)必须具有一个有效的非空URL值,该值可能被空格包围。