Skip to content

Instantly share code, notes, and snippets.

@nmoliveira
Created June 25, 2014 15:45
Show Gist options
  • Save nmoliveira/37a6e56a65b9875160d3 to your computer and use it in GitHub Desktop.
Save nmoliveira/37a6e56a65b9875160d3 to your computer and use it in GitHub Desktop.
Javascript function post to URL
/**
* This function makes a post to a given url with the given parameters
* The form is created on the fly and also the input elements
* @path the URL path
* @params the URL query parameters
*/
function postToUrl(path, params) {
var form = document.createElement("form");
form.setAttribute("method", 'post');
form.setAttribute("action", path);
for(var key in params) {
if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment