Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yanli0303/9026ad3e5a223856f277ad637d07e453 to your computer and use it in GitHub Desktop.
Save yanli0303/9026ad3e5a223856f277ad637d07e453 to your computer and use it in GitHub Desktop.
Open a new browser window using HTTP POST method.
/**
* Submit login form with POST method.
* @param {string} action The login page URL.
* @param {string} target The name of the iframe, or `_blank`;
* @param {object} params The dictionary of form data.
* @returns The form element inserted into the page.
*/
export const postForm = (action, target, params) => {
const form = document.createElement('form')
form.style.display = 'none'
form.setAttribute('method', 'post')
form.setAttribute('action', action)
form.setAttribute('target', target)
Object.entries(params).forEach(([key, value]) => {
const input = document.createElement('input')
input.setAttribute('type', 'text')
input.setAttribute('name', key)
input.value = value
form.appendChild(input)
})
document.body.appendChild(form)
form.submit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment