npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

formdata-submitter-polyfill

v1.0.3

Published

Support the submitter parameter to the FormData constructor in older browsers.

Downloads

613

Readme

FormData submitter polyfill

Support the submitter parameter to the FormData constructor in older browsers.

Usage

npm install --save formdata-submitter-polyfill

Then import it early in your client entrypoint .js file, e.g.

import "formdata-submitter-polyfill";

Now you can reliably create FormData objects populated from both a form and a submitter. A common scenario for this is in form submission handlers, e.g.

var myform = document.createElement("form");
myform.innerHTML = `
  <input name=foo value=FOO>
  <button name=go value=GO>go!</button>
  <input type=image>
  <input name=bar value=BAR>
`;
myform.addEventListener("submit", (event) => {
  event.preventDefault();
  const formData = new FormData(event.target, event.submitter);
  // If the button is the submitter:
  // ▸ FormData(3) { foo → "FOO", go → "GO", bar → "BAR" }
  // If the image button is the submitter:
  // ▸ FormData(4) { foo → "FOO", x → "0", y → "0", bar → "BAR" }

  // ... do something with formData ...
});

These FormData objects are equivalent to the form data sets constructed by equivalent native form submissions.

If you also need to polyfill the submitter property of the SubmitEvent, consider using the event-submitter-polyfill package alongside this one.

TypeScript

The latest TypeScript DOM types support the submitter parameter. If you are using older DOM types (e.g. the ones that shipped with your version of TypeScript), you can get the latest by running:

npm install @typescript/lib-dom@npm:@types/web --save-dev

If for some reason you can't upgrade yet, in the meantime you can add a // @ts-expect-error comment to make TypeScript happy 🙈.

Additional Exports

If you want more control over how/when the polyfill is activated, you can use the exports provided by formdata-submitter-polyfill/impl. For example:

import {
  FormData,
  polyfillFormDataIfNecessary,
} from "formdata-submitter-polyfill/impl";

// this will replace window.FormData with the polyfill if necessary
polyfillFormDataIfNecessary(FormData);

Lightweight mode

By default, the polyfill fully supports the form entry list construction algorithm, i.e. ensuring that image button and named button submitters are encoded in tree order. It accomplishes this by temporarily tweaking the form during submission to get the right entries.

While this performs well, you can get faster (but less compliant) behavior by doing:

import "formdata-submitter-polyfill/lite";

This will instead create a submitter-less FormData object and then append submitter entries (as appropriate) to the end of the list, e.g.

const formData = new FormData(event.target, event.submitter);
// If the button is the submitter:
// ▸ FormData(3) { foo → "FOO", bar → "BAR", go → "GO" }
// If the image button is the submitter:
// ▸ FormData(4) { foo → "FOO", bar → "BAR", x → "0", y → "0" }

Gotchas

  1. If the entry order matters for consumers, this may lead to bugs (i.e. the order will differ from one created from a vanilla form submission).

  2. Older versions of Safari will include submitter entries twice, due to this bug.

The default polyfill mode does not have these issues.