JavaScript Post

The three ways that the HTTP protocol provides for passing information between web pages are to use cookies, put the data in the querystring on the end of the address (GET) or to use POST. With regard to sending the data HTML itself can send data from any link within the page using the querystring and can send the data from a form using either GET or POST. To send data from the page via a cookie requires the use of JavaScript. JavaScript is also needed if you want the page inside the browser to be able to read a cookie or the querystring. There is no way to read POST data from within the web page in the browser.

While you can't read POST data using JavaScript, you can use JavaScript to send POST data from a web page without there being a visible form within the page. The following toPost() function accepts a destination address including a querystring as its single parameter and will transfer to the specified web page passing the values from the querystring as POST values rather than as GET values. It does this by extracting all the values from the querystring and creating a temporary form from JavaScript that contains hidden fields with all the appropriate names and values corresponding to the querystring content. Having created the form it then submits it which will load the page specified in the address portion of the supplied parameter passing the form content using POST. Since the submission of the form will result in a new page being loaded we do not even need to worry about deleting the temporary form as the page it is in will be overwritten by the loading of the new page.


now you can simply convert any web address with a query string attached to use post instead simply by passing it to the toPost() function. For example you could hard code it into a link using:

<a href="somewhere.html?a=1&b=2" onclick="toPost(this.href); return false;">


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow