当前位置:网站首页>How to dynamically add script dependent scripts
How to dynamically add script dependent scripts
2022-08-04 04:17:00 【act】
如何动态添加script依赖的脚本
使用的背景
在我们平常开发中,会在HTMLFile will be placed some rely on the script,但在有些情况下,Distinguish between different environment,While loading different script,Or the same script and different versions of,It's need to dynamically add
The concrete placement method
可以动态的创建script元素,The script then link tosrc,但要注意一点,After finish loading the script to the next step,Or placed script may not take effect or has been performed and rely on the script is still in loading.这是很重要的一点.
我所使用的框架是vit+react+ts进行开发的,So I added dynamicallyscriptElement is placed in theapp.tsx中.具体代码如下:例 Dynamic loading different versions of WeChatsdk版本
import React from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter } from 'react-router-dom';
import {
getEnv } from '@/utils';//Distinguish between WeChat environment and enterprise WeChat environment
import App from './app';
// According to different environment insertjs-sdk,According to the current document enterprise WeChat insert1.2,WeChat insert1.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')
);
}
边栏推荐
- FFmpeg —— 通过修改yuv,将视频转为黑白并输出(附源码)
- 【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】
- Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
- 杭电多校-Slipper-(树图转化+虚点建图)
- 劝退背后。
- 用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
- 什么是数字孪生智慧城市应用场景
- FPGA parsing B code----serial 3
- 目标检测-中篇
- 【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
猜你喜欢
Shell 函数
复现20字符短域名绕过
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
自定义通用分页标签01
网络工程师入门必懂华为认证体系,附系统学习路线分享
42. 接雨水
企业直播风起:目睹聚焦产品,微赞拥抱生态
7-2 LVS+DR Overview and Deployment
Introduction to mq application scenarios
汇编语言之栈
随机推荐
A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
3000字,一文带你搞懂机器学习!
Jenkins 导出、导入 Job Pipeline
Polygon zkEVM network node
MySQL query optimization and tuning
42. 接雨水
7.LVS负载均衡群集之原理叙述
7-2 LVS+DR概述与部署
Converts XML tags to TXT format (voc conversion for yolo convenient training)
drools从下载到postman请求成功
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
Reproduce 20-character short domain name bypass
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
Embedded database development programming MySQL (full)
2003. 每棵子树内缺失的最小基因值 DFS
For Qixi Festival, I made a confession envelope with code
Stop behind.
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
Implementing a server-side message active push solution based on SSE
Take care of JVM performance optimization (own note version)