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

dynamic-loading-script

v1.0.9

Published

Dynamic script loading.

Downloads

5

Readme

dynamic-loading-script

动态加载脚本,并根据脚本的加载情况,执行回调函数。

  • 如果脚本已经加载,直接执行回调函数,避免重复加载
  • 如果脚本未加载,先加载脚本,再执行回调函数

注意事项

  • 异步加载js - 如果js已经加载过,则直接返回 如果cdn不是直接返回js类方法,而是采用jsonp的方式, 请务必传入jsonpCallbackName参数

安装

npm install dynamic-loading-script --save
yarn add dynamic-loading-script --save

使用

参数

| 参数 | 类型 | 说明 | 默认值 | 必填 | | ---------------------- | --------------- | ------------------------------------------------------------ | ------ | ---- | | Url | String | 脚本的url地址 | 无 | 是 | | opts | Object | 配置项 | 无 | 是 | | opts.variableName | String | 脚本中的变量名,如果脚本中有变量名,可以通过该参数获取到该变量 | 无 | 是 | | opts.callback | Function | 脚本加载完成后的回调函数, 此属性存在时 | 无 | 否 | | opts.jsonpCallbackName | String | 脚本的jsonp方法名 | 无 | 否 | | opts.timeout | number | 是否在超时之后移除script标签 | false | 否 |

使用jsonp的形式参见jsonp示例

// 引入改js后,可以直接获取js库变量(类)的例子,
import dynamicLoadScript from 'dynamic-loading-script'

dynamicLoadScript('https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js', {
    variableName: 'axios',
    callback:(axios) => {
        console.log('axios loaded', axios)
        console.log('axios', window.axios)
    }
})

import dynamicLoadScript from "dynamic-loading-script";
dynamicLoadScript( "https://cdn.bootcdn.net/ajax/libs/echarts/5.0.2/echarts.min.js",
  {
    variableName: "echarts",
  }
).then((echarts) => {
  console.log(echarts);
});

注意

  • $已经为window已存在的变量,故动态加载jquery不会成功