Jump to content

diff between action="#" and action=""


jefals

Recommended Posts

Hi,In my beginning javascript book, sometimes the examples have html where the form action attribute is action = "#" and sometimes it's action = "". Of course, we're not really opening any live urls -- just running exercises stored on my computer. But, the HTML book I already went thru (Murachs), doesn't mention either of these values for the action attribute. Is there a difference? Thanks!,Jeff S

Link to comment
Share on other sites

The action attribute of a form is the destination to which the form should be submitted. Usually it is the URL of a script on your server.HTTP rules say the attribute is required, and a form without one will not validate.When the attribute is the empty string, the form will be submitted to itself. That is, the browser will send the form data to the document on the server with the same URL as the document containing the form. This is very common with login documents, since a failed login usually results in the login form being returned anyway. So many developers put the login logic in the same document as the login form. (Of course, the server-side code is not downloaded, so most users do not know this.)If there is no server-side logic in the document, the document will simply reload. Since most browsers repopulate all the form fields in such a case, it is often difficult to know that this has happened. But it has.When the attribute is "#" the form is not submitted at all. (Haris S is incorrect when he says there is no difference.) The browser behaves much as it would if you click a link whose href is set to "#". Nothing much seems to happen. Misguided developers often use this when they want a link to trigger some javascript and don't want to load another page in the browser. (There are better ways to accomplish the same thing.)The real purpose of "#" in an address is to send the browser to an anchor inside the document itself. Properly used, you might see something like this: <a href="#chapter2">Chapter 2</a> . Clicking that would scroll the page to an anchor that looks like this <a name="chapter2"></a> . A lone "#" in place of a complete reference scrolls the browser to nowhere. (Usually there is an annoying jump of several pixels, though.)Anyway, using action="#" has a similar effect. The form does not submit. Nothing seems to happen. If for some reason your attribute looked like this: action="#chapter2", a submit event would cause the browser to scroll to any anchor named "chapter2". Pointless behavior, for all I can tell, but that's what happens.Be aware that if you want to use form inputs to trigger JavaScript behavior, but you do not want to submit a form anywhere, there is no requirement to put the inputs inside a form element, so the form tags can simply be omitted. This is semantically preferable to a form that does not behave like a form.

Link to comment
Share on other sites

The action attribute of a form is the destination to which the form should be submitted. Usually it is the URL of a script on your server.HTTP rules say the attribute is required, and a form without one will not validate.When the attribute is the empty string, the form will be submitted to itself. That is, the browser will send the form data to the document on the server with the same URL as the document containing the form. This is very common with login documents, since a failed login usually results in the login form being returned anyway. So many developers put the login logic in the same document as the login form. (Of course, the server-side code is not downloaded, so most users do not know this.)If there is no server-side logic in the document, the document will simply reload. Since most browsers repopulate all the form fields in such a case, it is often difficult to know that this has happened. But it has.When the attribute is "#" the form is not submitted at all. (Haris S is incorrect when he says there is no difference.) The browser behaves much as it would if you click a link whose href is set to "#". Nothing much seems to happen. Misguided developers often use this when they want a link to trigger some javascript and don't want to load another page in the browser. (There are better ways to accomplish the same thing.)The real purpose of "#" in an address is to send the browser to an anchor inside the document itself. Properly used, you might see something like this: <a href="#chapter2">Chapter 2</a> . Clicking that would scroll the page to an anchor that looks like this <a name="chapter2"></a> . A lone "#" in place of a complete reference scrolls the browser to nowhere. (Usually there is an annoying jump of several pixels, though.)Anyway, using action="#" has a similar effect. The form does not submit. Nothing seems to happen. If for some reason your attribute looked like this: action="#chapter2", a submit event would cause the browser to scroll to any anchor named "chapter2". Pointless behavior, for all I can tell, but that's what happens.Be aware that if you want to use form inputs to trigger JavaScript behavior, but you do not want to submit a form anywhere, there is no requirement to put the inputs inside a form element, so the form tags can simply be omitted. This is semantically preferable to a form that does not behave like a form.
Link to comment
Share on other sites

Sorry...didn't mean to copy your post. I was trying to copy /paste part of it for further discussion, and guess i hit the wrong button or something.I really appreciate your taking the time to provide such an articulate, well thought out response. As I am so new to this --- the extent of my knowledge is that I have been thru a 500 page beginner's book on html/css, and am now a little over1/2 way thru a beginners javascript. No practical experience, and no exposure at all to anything server side yet.So, you'll realize I had a hard time understanding a lot of what you were telling me. In all the work I'm doing so far, there is no server side logic. So, you said below, in that case, when the action attribute is empty, the document will reload. You're talking about, when I hit the reload on the browser, right? Does the "onsubmit" event also trigger that reload? Then, if the action is "#", the form is not submitted at all. But it still reloads if I hit the browser reload, right? If I understand correctly, the difference is with the "onsubmit" event; In the case of the empty action, it causes a reload, and in the case of the "#", it just doesn't do anything; Is this correct?Thanks!,Jeff

Link to comment
Share on other sites

the difference is with the "onsubmit" event; In the case of the empty action, it causes a reload, and in the case of the "#", it just doesn't do anything;
Yes. I was discussing what happens when a submit event fires, not when the user reloads/refreshes. It might have been more clear if I'd said something about hitting the submit button, but there are other ways a submit event can fire.Under normal circumstances, the browser will always reload/refresh the page when you hit the correct button/menu item, since all that is outside the document.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...