Skip to content

Include

<quiet-include> stable since 1.0

Includes let you pull content from another file into your page.

<quiet-include src="/assets/examples/include.html"></quiet-include>

The file you're including must be from a CORS-enabled endpoint.

Examples Jump to heading

Default content Jump to heading

You can provide default content inside the include, which will be shown until the request completes. This is useful to show a loading state while the include file is fetched.

Loading content…
<quiet-include src="/assets/examples/include.html">
  Loading content…
</quiet-include>

Allowing scripts Jump to heading

Scripts included in the fetched HTML will not be executed by default. If you trust the content, you can use the allow-scripts attribute to allow them to run.

<quiet-include allow-scripts src="/assets/examples/include-scripts.html"></quiet-include>

Using this option can be dangerous! Make sure you trust the included content, otherwise your app may become vulnerable to XSS exploits!

Handling events Jump to heading

When an include file is successfully rendered, the quiet-included event will be emitted. When things go wrong, there are two possible events that signal an error.

If the HTTP request completes but the response is outside of the acceptable 200 range, the quiet-http-error event will be emitted. If the HTTP request fails due to a network error, CORS restriction, or similar, the quiet-network-error event will be emitted.

<quiet-include src="/assets/examples/include.html" id="include__events"></quiet-include>

<script>
  const include = document.getElementById('include__events');

  include.addEventListener('quiet-included', () => {
    //
    // The include file has rendered
    //
  });

  include.addEventListener('quiet-http-error', event => {
    //
    // The request completed, but the HTTP code was outside of 
    // the 200 range. You can inspect event.detail.status to
    // see which HTTP code was provided.
    //
  });

  include.addEventListener('quiet-network-error', event => {
    //
    // The request could not be completed. You can inspect 
    // event.detail.error to see the response error.
    //
  });
</script>

API Jump to heading

Importing Jump to heading

The autoloader is the recommended way to import components but, if you prefer to do it manually, the following code snippets will be helpful.

CDN npm

To manually import <quiet-include> from the CDN, use the following code.

import 'https://cdn.jsdelivr.net/npm/@quietui/quiet@1.0.0/dist/components/include/include.js';

To manually import <quiet-include> from npm, use the following code.

import '@quietui/quiet/dist/components/include/include.js';

Properties Jump to heading

Include has the following properties that can be set with corresponding attributes. In many cases, the attribute's name is the same as the property's name. If an attribute is different, it will be displayed after the property. Learn more about attributes and properties

Property / Attribute Description Reflects Type Default
src The URL of the file to include. Must be a CORS-enabled endpoint. string
mode The mode to use when fetching the request. RequestMode 'cors'
allowScripts
allow-scripts
By default, scripts in included files will not be executed. Setting this to true will allow them to run. If you use this option, make sure you trust the included HTML, otherwise you may become vulnerable to XSS exploits. boolean false

Events Jump to heading

Include dispatches the following custom events. You can listen to them the same way was native events. Learn more about custom events

Name Description
quiet-included Emitted when the include file has been fetched and rendered. The HTTP status code will be available in event.detail.status. This event does not bubble.
quiet-include-error Emitted when the fetch results in a network error or receives an HTTP response outside of the 200 range. If a network error occurs, it will be available in event.detail.error. If an HTTP status code was returned, it will be available in event.detail.status. This event does not bubble.
Search this website Toggle dark mode Get the code on GitHub Follow @quietui.org on Bluesky Follow @quiet_ui on X
    No results found