Function loadScript

  • EN: Load a JavaScript file from the server and execute it.

    ZH: 动态加载 JavaScript 文件

    Usage:

    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`
    isDefer: false, // (Optional) defer, default `false`
    }
    )
    .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

      JavaScript 资源路径

    • options: {
          callback?: ((...params) => any);
          id?: string;
          isDefer?: boolean;
          timeout?: number;
      } = ...
      • Optional callback?: ((...params) => any)
          • (...params): any
          • 加载后回调函数

            Parameters

            • Rest ...params: UnknownFnParams

            Returns any

      • Optional id?: string

        DOM ID

      • Optional isDefer?: boolean

        是否添加 defer 标签

      • Optional timeout?: number

        超时时长

    Returns LoadScriptReturns

    -- true 成功