Is it a good practice to use an empty URL for a HTML form's action attribute? (action=“”)

后端 未结 11 2196
花落未央
花落未央 2020-11-22 01:50

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 blan

相关标签:
11条回答
  • 2020-11-22 02:12

    I used to do this a lot when I worked with Classic ASP. Usually I used it when server-side validation was needed of some sort for the input (before the days of AJAX). The main draw back I see is that it doesn't separate programming logic from the presentation, at the file level.

    0 讨论(0)
  • 2020-11-22 02:14

    When you put empty action then some security filtration consider it malicious or phishing. Hence they can block your page. So its advisable not to keep action= blank.

    0 讨论(0)
  • 2020-11-22 02:16

    I think it's best to explicitly state where the form posts. If you want to be totally safe, enter the same URL the form is on in the action attribute if you want it to submit back to itself. Although mainstream browsers evaluate "" to the same page, you can't guarantee that non-mainstream browsers will.

    And of course, the entire URL including GET data like Juddling points out.

    0 讨论(0)
  • 2020-11-22 02:16

    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.

    0 讨论(0)
  • 2020-11-22 02:17

    Just use

    ?

    <form action="?" method="post" enctype="multipart/form-data" name="myForm" id="myForm">
    

    It doesn't violate HTML5 standards.

    0 讨论(0)
  • 2020-11-22 02:18

    Actually, the Form Submission subsection of the current HTML5 draft does not allow action="". It is against the spec.

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

    The quoted section in mercator's answer is a requirement on implementations, not authors. Authors must follow the author requirements. To quote How to read this specification:

    In particular, there are conformance requirements that apply to producers, for example authors and the documents they create, and there are conformance requirements that apply to consumers, for example Web browsers. They can be distinguished by what they are requiring: a requirement on a producer states what is allowed, while a requirement on a consumer states how software is to act.

    The change from HTML4—which did allow an empty URL—was made because “browsers do weird things with an empty action="" attribute”. Considering the reason for the change, its probably best not to do that in HTML4 either.

    0 讨论(0)
提交回复
热议问题