Last active
August 3, 2024 22:53
-
-
Save cgarciagl/3744034 to your computer and use it in GitHub Desktop.
function to redirect a webpage to another using post method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function redirectByPost(url, parameters, inNewTab) { | |
parameters = parameters || {}; | |
inNewTab = inNewTab === undefined ? true : inNewTab; | |
var form = document.createElement("form"); | |
form.id = "reg-form"; | |
form.name = "reg-form"; | |
form.action = url; | |
form.method = "post"; | |
form.enctype = "multipart/form-data"; | |
if (inNewTab) { | |
form.target = "_blank"; | |
} | |
Object.keys(parameters).forEach(function (key) { | |
var input = document.createElement("input"); | |
input.type = "text"; | |
input.name = key; | |
input.value = parameters[key]; | |
form.appendChild(input); | |
}); | |
document.body.appendChild(form); | |
form.submit(); | |
document.body.removeChild(form); | |
return false; | |
} |
Thank you, now the function has been improved, and the dependency on JQuery has been removed. 😉
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do not use raw HTML at line 12. Instead, create the input element and set its value programmatically:
Using raw HTML do not work with " and line breaks