当前位置:网站首页>如何动态添加script依赖的脚本
如何动态添加script依赖的脚本
2022-08-04 03:47:00 【行致】
如何动态添加script依赖的脚本
使用的背景
在我们平常开发中,会在HTML文件中会放置一些所依赖的脚本,但在有些情况下,区分不同的环境下,而加载不同的脚本,或者相同的脚本而不同的版本,这就需要动态添加了
具体放置方法
可以动态的创建script元素,然后脚本链接赋值给src,但要注意一点,在加载完这个脚本之后才能进行下一步,否则放置的脚本可能没有生效或者是已经执行完而所依赖的脚本还在加载中。这是很重要的一点。
我所使用的框架是vit+react+ts进行开发的,因此我动态添加的script元素是放置在app.tsx中。具体代码如下:例 动态加载不同版本的微信sdk版本
import React from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter } from 'react-router-dom';
import {
getEnv } from '@/utils';//区分微信环境和企业微信环境
import App from './app';
// 根据环境插入不同js-sdk,按目前文档企业微信插入1.2,微信插入1.6
const script = document.createElement('script');
// script.crossOrigin = 'anonymous';
if (getEnv() === 'qywx') {
script.src = 'https://res.wx.qq.com/open/js/jweixin-1.2.0.js';
script.onerror = () => {
window.$log('qy-wx-sdk:loadError');
};
} else {
script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js';
script.onerror = () => {
window.$log('wx-sdk:loadError');
};
}
script.onload = rcInit;//加载完script后,渲染页面dom
document.head.appendChild(script);
function rcInit() {
ReactDOM.render(
<BrowserRouter basename="/admin-app">
<App />
</BrowserRouter>,
document.getElementById('root')
);
}
边栏推荐
- 本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
- 用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
- 怎样提高网络数据安全性
- new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
- Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
- 从图文展示到以云为核,第五代验证码独有的策略情报能力
- DIY电工维修如何拆卸和安装开关面板插座
- MySQL查询优化与调优
- Architecture of the actual combat camp module three operations
- SQL interview Questions
猜你喜欢
if,case,for,while
如果禁用了安全启动,GNOME 就会发出警告
Polygon zkEVM network node
Introduction to mq application scenarios
docker+bridge+redis master-slave+sentry mode
4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
STM8S project creation (STVD creation) --- use COSMIC to create a C language project
跨境电商看不到另一面:商家刷单、平台封号、黑灰产牟利
new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
How to systematically plan and learn software testing?
随机推荐
How class only static allocation and dynamic allocation
View mysql deadlock syntax
Metaverse "Drummer" Unity: Crazy expansion, suspense still exists
技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
How to systematically plan and learn software testing?
解决问题遇到的问题
Mockito单元测试
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机
Oracle与Postgresql在PLSQL内事务回滚的重大差异
基于Qt的目录统计QDirStat
6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
Architecture of the actual combat camp module three operations
STM8S105k4t6c---------------Light up LED
【观察】超聚变:首提“算网九阶”评估模型,共建开放繁荣的算力网络
企业直播风起:目睹聚焦产品,微赞拥抱生态
MySQL query optimization and tuning
哎,又跟HR在小群吵了一架!
网络工程师入门必懂华为认证体系,附系统学习路线分享
Brush esp8266-01 s firmware steps