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

lazy-scripts

v0.3.2

Published

load, parse and execute scripts when needed

Downloads

17

Readme

lazy-scripts

Like you do with lazy loaded images, lazy load, parse and execute your js.

Quick Start

Include lazy-scripts.min.js somewhere in your page, and call new LazyScripts(); at the end of your page, or whenever your page is loaded.

Add one of the following data--attributes to your markup:

  <div class="my-slider" data-lazy-script="/path/to/my-slider.js">…</div>

or

  <div class="my-slider" data-lazy-scripts='["/path/to/a/3rd-party/slider-lib.js","/path/to/my-slider.js"]'>…</div>

!important: take care of JSON.parse compatible Array-String when you use data-lazy-scripts.

and that's it.

As soon as your html element enters the viewport, the js is loaded and executed.

ES-6 usage

import LazyScripts from 'lazy-scripts';

new LazyScripts();

ES-5 usage

<script src="lazy-scripts.min.js"></script>
<script>new LazyScripts();</script>

Options

The constructor call new LazyScripts() supports an option object like this:

  new LazyScripts({
    lazyScriptSelector: '[data-load-script]',
    lazyScriptsSelector: '[data-load-scripts]'
  });

by default the scripts will query your DOM for data-lazy-script for a single js file, and data-lazy-scripts for array notated scripts, load, parsed and executed in array order.

CustomEvents (since 0.2.2)

LazyScripts fires on every successful loaded script a lazyScriptLoaded CustomEvent on the body element, you can catch and do something with it.

  …
  function lazyCallback(event) {
    console.log(event.detail.scriptSrc); // path of actual added script
  }

  document.body.addEventListener('lazyScriptLoaded', lazyCallback);
  …

CustomEvents (since 0.2.3)

I de-include the CustomEvent polyfill I added in 0.2.2. LazyScripts still dispatches CustomEvents if your browser supports it. It is up to you to include (or no) the polyfill you like the most.

MutationObserver (since 0.3.0)

With 0.3.0 a MutationObserver watches your DOM changes and will trigger lazy scripts on DOM changes. No work needed on your side.

Script Adjustments

If your script(s) wait for a DOMContentLoaded or window.load you need to change that. These Events will be long forgotten when your scripts will be loaded, parsed and executed.

If you use Custom Events, this should be not a problem, the script is embedded in your page and will catch every event you fire.

Browser Support

lazy-scripts is a plain js library. It utilises IntersectionObserver but when no IntersectionObserver is found it will load all scripts directly. Your choice to include a IntersectionObserver polyfill or not.

For IE11 mdn-polyfills/NodeList.prototype.forEach is included, so you can use umd minified source out of the box.

With 0.2.2 mdn-polyfills/CustomEvent is added.

With 0.2.3 CustomEvent polyfill was removed. Include it by yourself if you need it.