为HTML表单的action属性使用空的URL是一种好的做法吗? (动作= QUOT;&QUOT) [英] Is it a good practice to use an empty URL for a HTML form's action attribute? (action="")

查看:178
本文介绍了为HTML表单的action属性使用空的URL是一种好的做法吗? (动作= QUOT;&QUOT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里有一个帖子询问一个空白的HTML表单动作在这里执行和一些页面,如这一个暗示它很好,但我想知道人们的想法。

解决方案

您可以做的最好的事情是完全省略action属性。如果您不填写,表单将提交到文档的地址,即相同的页面。



也可以将其保留为空,并且任何执行< HTML的表单提交算法将其视为等同于文档的地址,主要是因为浏览器目前的工作方式:


8 。让action为submitter元素的 action


$ b

9。如果action是空字符串,让行动成为文件的地址



注意:这一步是 RFC 3986的故意违规,这将需要在这里进行基本的URL处理。这种违规行为的动机是希望与传统内容兼容。 [RFC3986]

这在所有当前浏览器中都可以正常工作,但在某些旧浏览器中可能无法正常工作( 浏览器做一个空行为奇怪的事情=属性),这就是为什么规范强烈建议作者不要将它留空 code> action 和 formaction 内容属性(如果指定)必须具有值潜在的有效非空网址被...围绕空格



I am wondering if anyone can give a "best practices" response to using blank HTML form actions to post back to the current page.

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.

解决方案

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.

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. Let action be the submitter element's action.

9. If action is the empty string, let action be the document's address.

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]

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:

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是一种好的做法吗? (动作= QUOT;&QUOT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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