mazey
    Preparing search index...

    Function loadScript

    • Load and execute a JavaScript file dynamically.

      Usage:

      import { loadScript } from "mazey";

      loadScript(
      "http://example.com/static/js/plugin-2.1.1.min.js",
      {
      id: "iamid", // (Optional) script ID, default none
      timeout: 5000, // (Optional) timeout, default `5000`
      }
      )
      .then(
      res => {
      console.log(`Load JavaScript script: ${res}`);
      }
      )
      .catch(
      err => {
      console.error(`Load JavaScript script: ${err.message}`);
      }
      );

      Output:

      Load JavaScript script: loaded
      

      Parameters

      • url: string

        URL of the JavaScript resource.

      • options: {
            attributes?: Record<string, string> | null;
            callback?: (...params: MazeyFnParams) => any;
            cssUrl?: string;
            id?: string;
            isAsync?: boolean;
            isCrossOrigin?: boolean;
            isDefer?: boolean;
            timeout?: number;
        } = ...
        • Optionalattributes?: Record<string, string> | null

          Additional attributes for the <script> element.

        • Optionalcallback?: (...params: MazeyFnParams) => any

          Callback invoked after the script loads.

        • OptionalcssUrl?: string

          Optional CSS resource to begin loading before the script is inserted.

        • Optionalid?: string

          Optional ID for the <script> element.

        • OptionalisAsync?: boolean

          Whether to set the async attribute.

        • OptionalisCrossOrigin?: boolean

          Whether to request the script anonymously across origins.

        • OptionalisDefer?: boolean

          Whether to set the defer attribute.

        • Optionaltimeout?: number

          Timeout in milliseconds.

      Returns LoadScriptReturns

      A promise that resolves to "loaded" after the script loads.