4

Now I do that in this way.

var form=document.createElement('form');
form.setAttribute('method', 'get');
form.setAttribute('action', 'url');
hidden=document.createElement('input');
hidden.setAttribute('type', 'hidden');
hidden.setAttribute('name', 'name');
hidden.setAttribute('value', 'value');
form.appendChild(hidden);
form.submit();

But I want save the cost on creating the DOMs

Is it possible to submit post request in javascript without DOM?

3
  • What is your question? Commented Jun 15, 2013 at 20:13
  • If you're concerned about the performance hit of manipulating the DOM; why not have the form hidden. So it's already in the DOM? Commented Jun 15, 2013 at 20:14
  • Why do you want to avoid Ajax (XMLHttpRequest)? That would save you the (low) cost of creating DOM elements Commented Jun 15, 2013 at 20:26

2 Answers 2

3

Yes.

document.forms[0].submit()

submits the 1st form on the page. And it is the typical way to submit a form in a "non-ajax" way. But the term "non ajax" is misleading in your question, hence the "POST"-verb is a simple http-verb. And there is no difference in using it "ajax"-way or a "non-ajax" way. The artificial difference one could make is: "application/x-www.formurlencoded" is the preferred format of the browser or "application/json" as you are doing it with "ajax".

P.S.: It is hard to answer your question. Of course you could avoid the dynamic creation of a form-element; but you have to use a form element anyways to do a non-ajaxy submit.

Sign up to request clarification or add additional context in comments.

4 Comments

"And there is no difference in using it "ajax"-way or a "non-ajax" way." Sure there is. Either you use XMLHTTPRequest or you submit the form normally. ""application/x-www.formurlencoded" is the preferred format of the browser or "application/json" as you are doing it with "ajax"." Very often the data sent via an Ajax request is form-urlencoded as well, since that how form data is normally encoded (related: FormData). The data format does not have much to do with the transport you choose.
hm. But where is the difference between XMLHTTPRequest and an usual "submit" from the http-point of view? As far as I can see POST is POST, independend from how it was technically initiated. And of course you are right, that very often data sent via ajax could be form-urlencoded; therefore i wrote it would be "artificial".
Of course there is no difference regarding HTTP, but regarding the JavaScript side. But I understand where you are coming from. The title says "non-ajax post" and of course there is no such thing (POST is POST, as you said). I was more focused on the "JavaScript" point of view. I guess that's the difference how we saw the question :)
I come from "down under" so to say G The term "non ajax-post" is somehow "nonsensical".
1

I believe you won't actually take much a performance hit doing it the way you are. The big issues with editing the DOM is actually adding to the document as it requires re-rendering and stuff.

1 Comment

DOM manipulation like this is super trivial. Elapsed time: 0ms (read: inconsequential) :D While XHR is generally preferred there are advantages to this approach such as returning "Download Files" or "Upload Files via IFrame" (for browsers without such support in XHR/XDR).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.