sf-utils2 sf-utils2
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
  • 快速开始

  • 基础-Base

  • 对象-Object

  • 数组-Array✨✨✨

  • 方法-Function

  • 字符串-String

  • 数学-Math

  • 文件-Buffer

  • 节点-dom

    • 序言 👏
    • _constant
    • getHttpBlob【获取远程二进制数据流】
    • downloadFile【http下载文件】
    • loadJsSync【单个加载js文件】
      • 1.示例
        • 1.1 加载单个 js
      • 2.入参说明
      • 3.源码
    • loadJsOrCssMulSync【批量加载远程css和js文件】
    • getAbsOffsetTop【距离父滚动区域顶部绝对距离】 🔥
    • getParentScrollElement【获取父元素滚动元素】 🔥
    • isScroll【是否是滚动容器】
    • scrollToX【水平滚动到某个具体位置】 🔥
    • scrollToY【垂直滚动到某个具体位置】 🔥
    • scrollToElement【滚动到某个dom元素】 🔥
    • isElementInContainer【元素是否整体在容器内】
    • batchElsPosInContainer【批量 dom 元素在 父的滚动视图位置】 🔥
    • isElementVisibleInViewport【是否可见在某个股东元素上】 🔥
    • observerElementMutation【监听dom元素属性变化】 🔥
    • print【打印】 🔥
    • partialCb【分片加载】
    • domUtils【dom方法操作基础】 🔥
  • 拓展

  • nodejs

目录

loadJsSync【单个加载js文件】

描述

支持单个加载远程 js 文件,返回 promise,如果 html 已经出现相同的 script,将不会重新加载,除非 src 地址不一致。 如果批量加载 js 和 css,请使用 loadJsOrCssMulSync

# 1.示例

# 1.1 加载单个 js

加载远程jquery.js 打印 $
<template>
  <div v-loading="loading">
    <el-button @click="onClick">加载远程jquery.js</el-button>
    <el-button @click="onPrint">打印 $</el-button>
  </div>
</template>

<script>
import { loadJsSync } from 'sf-utils2'
const jquery = 'https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js'
export default {
  data() {
    return {
      loading: false
    }
  },
  methods: {
    async onClick() {
      this.loading = true
      await loadJsSync(jquery)
      this.$message.success('加载成功')
      this.loading = false
    },

    onPrint() {
      console.log(window.$)
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
显示代码 复制代码 复制代码

# 2.入参说明

参数 说明 类型 是否必填 默认值
url js String 是
callback 加载成功后,回调函数 Function 否

# 3.源码

源码,点开查看 👈
/**
 * 加载js 支持持的单个
 * @param {string} url js http链接🔗地址
 * @param callback  加载成功回调函数
 */
function loadJsSync(url = '', callback) {
  return new Promise(resolve => {
    let script = document.createElement('script'),
      fn = callback || function () {}
    script.type = 'text/javascript'
    //IE
    if (script.readyState) {
      script.onreadystatechange = function () {
        if (script.readyState == 'loaded' || script.readyState == 'complete') {
          script.onreadystatechange = null
          fn()
          resolve()
        }
      }
    } else {
      //其他浏览器
      script.onload = function () {
        fn()
        resolve()
      }
    }
    script.src = url
    document?.getElementsByTagName('head')[0].appendChild(script)
  })
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
上次更新: 2025/07/01, 14:52:29
downloadFile【http下载文件】
loadJsOrCssMulSync【批量加载远程css和js文件】

← downloadFile【http下载文件】 loadJsOrCssMulSync【批量加载远程css和js文件】→

Theme by Vdoing | Copyright © 2022-2025 bianpengfei
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×