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

appendhtml

v1.2.0

Published

Appends HTML to an element and makes sure <scripts> run properly

Downloads

260

Readme

appendHTML()

This library exports a single function that takes an HTML string and a container and will append the HTML to (put the html at the end of) the container. It also executes scripts appended, which may be useful when working with embed codes.

Why

Why is that special? Usually when you append HTML to a container, for instance using .innerHTML, any script tags in your HTML will not be executed. I.e. doing this:

const html = '<p>Hello</p><script>alert("world")</script>'; 
const container = document.getElementById('some-div');
container.innerHTML = html;

will never execute the JS and never alert "world". The same goes for script nodes with a src attribute.

What you would need to do is parse your HTML string and for each <script> tag create a script node with document.createElement('script'), copy its attributes and text contents and append it to the container. You'd also have to make sure that when you have mixed (regular) HTML and script nodes they get appended and executed in the right order and at the right time, accounting for inline scripts as well as scripts with and without the async attribute.

This library does just that for you and provides you with cjs, esm and umd modules thanks to rollup.js.

Installation

yarn add appendhtml

and then either of

import appendHtml from 'appendhtml';
const appendHtml = require('appendhtml');
// or <script src="node_modules/appendhtml/dist/index.umd.js"></script> which gives you a global function appendHtml

Usage

appendHtml takes 3 arguments

| Argument | Description | Default Value | | -------- | ----------- | ------------- | | html | A string of HTML possibly containing script nodes | none | | container | A DOM node to append the HTML to | none | | timeOut (optional!) | A timeout in milliseconds to wait for scripts to load before resolving the returned Promise. Note that the Promise will never be rejected, it will be resolved after the timeout because this is the behaviour of browsers. | 2000 |

The return value is a Promise<void> that resolves when all scripts have been loaded (or when scripts are async or the timeout is hit).

Examples

The first example, fixed

import appendHtml from 'appendhtml';
const html = '<p>Hello</p><script>alert("world")</script>'; 
const container = document.getElementById('some-div');
appendHtml(html, container); // alerts "world"

Wait for script to load

import appendHtml from 'appendhtml';
const html = '<p>Hello</p><script src="some_js_file.js"></script>'; 
const container = document.getElementById('some-div');
await appendHtml(html, container);
// appendHtml returns a Promise, some_js_file.js is now loaded and executed (note the await)

Fancy example

import appendHtml from 'appendhtml';
const response = await fetch('https://publish.twitter.com/oembed?url=https://twitter.com/luke_schmuke/status/766775290404233217');
const json = await response.json();
const container = document.getElementById('some-div');
await appendhtml(json.html, container);
// Hooray, we just embedded a tweet

Maintainers

Testing

Tests automated using BrowserStack for OSS

License

The license is MIT