form-request-submit-polyfill.js 1.0 KB

123456789101112131415161718192021222324252627
  1. (function(prototype) {
  2. if (typeof prototype.requestSubmit == "function") return
  3. prototype.requestSubmit = function(submitter) {
  4. if (submitter) {
  5. validateSubmitter(submitter, this)
  6. submitter.click()
  7. } else {
  8. submitter = document.createElement("input")
  9. submitter.type = "submit"
  10. submitter.hidden = true
  11. this.appendChild(submitter)
  12. submitter.click()
  13. this.removeChild(submitter)
  14. }
  15. }
  16. function validateSubmitter(submitter, form) {
  17. submitter instanceof HTMLElement || raise(TypeError, "parameter 1 is not of type 'HTMLElement'")
  18. submitter.type == "submit" || raise(TypeError, "The specified element is not a submit button")
  19. submitter.form == form || raise(DOMException, "The specified element is not owned by this form element", "NotFoundError")
  20. }
  21. function raise(errorConstructor, message, name) {
  22. throw new errorConstructor("Failed to execute 'requestSubmit' on 'HTMLFormElement': " + message + ".", name)
  23. }
  24. })(HTMLFormElement.prototype);