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

hijax-form

v1.1.0

Published

custom element for AJAX form submission

Downloads

18

Readme

hijax-form

package version build status

<hijax-form> is a custom element for AJAX form submission, relying on established HTML form semantics and leaving markup up to the server. Thus server-side implementations can remain agnostic of this client-side enhancement.

Its core functionality can also be used as a library (→ util.js), though the custom element is required to support named buttons.

Usage

<hijax-form cors>
    <form action="/items" method="post">
        <input type="text" name="message">
        <button>submit</button>
    </form>
</hijax-form>

The optional cors boolean attribute, if present, ensures credentials are included with cross-origin requests.

import "hijax-form"; // automatically registers custom element

let form = document.querySelector("form");
form.addEventListener("submit", function(ev) {
    this.closest("hijax-form").submit().
        then(response => response.text()).
        then(html => {
            // process response, e.g. by updating page contents
        });

    ev.preventDefault(); // suppress the browser's default form handling
});

While AJAX submission is in progress, an aria-busy="true" attribute will be added to <hijax-form>.

Caveats

  • file input fields are unsupported

    Attempting to serialize a form which includes <input type="file" …> will throw an exception. Thus it is recommended to invoke Event#preventDefault only after serialization has occurred, allowing the browser to fall back to regular (non-AJAX) form submission.

  • named buttons (i.e. <button name="…" …>) are supported by injecting a temporary substitute field (i.e. <input type="hidden" name="…" …>) - thus:

    • submit event handlers' form processing, i.e. invocation of the #serialize or #submit methods, must occur synchronously
    • if no submit event handler is registered, i.e. the browser's default handling is not suppressed, the temporary substitute field will result in the respective button's parameter being duplicated when the browser submits the form

Dependencies

hijax-form has no external dependencies (though it includes a few tiny utility functions from uitil).

However, it does rely on modern browser features which might need to be polyfilled:

The source code is written in ES2015, without relying on non-standardized language extensions.

Contributing

  • ensure Node is installed
  • npm install downloads dependencies
  • npm run compile performs a one-time compilation, generating dist/bundle.js
  • npm start automatically recompiles while monitoring code changes
  • npm test checks code for stylistic consistency
  • the test suite (test/index.html) needs to be opened in a browser

License

hijax-form is licensed under the Apache 2.0 License.