当前位置:网站首页>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')
);
}
边栏推荐
- Introduction to mq application scenarios
- docker安装mysql与宿主机相差8小时的问题。
- The general SQL injection flow (sample attached)
- 中信证券网上开户怎么开的?安全吗?
- 文件内容的操作
- 将xml标签转换为txt(voc格式转换为yolo方便进行训练)
- Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
- The video of machine learning to learn [update]
- The Shell function
- 7-3 LVS+Keepalived集群叙述与部署
猜你喜欢
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
7-3 LVS+Keepalived集群叙述与部署
The video of machine learning to learn [update]
drools from download to postman request success
Implementing a server-side message active push solution based on SSE
docker安装mysql与宿主机相差8小时的问题。
技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
share总结
Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
7.LVS负载均衡群集之原理叙述
随机推荐
SVM介绍以及实战
How class only static allocation and dynamic allocation
JVM的内存模型简介
6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
马尔可夫链
Jenkins 导出、导入 Job Pipeline
自定义通用分页标签02
机器学习模型的“可解释性”
Reproduce 20-character short domain name bypass
Jenkins export and import Job Pipeline
SQL injection in #, - +, - % 20, % 23 is what mean?
Converts XML tags to TXT format (voc conversion for yolo convenient training)
Introduction to mq application scenarios
杭电多校-Slipper-(树图转化+虚点建图)
Senior PHP development case (1) : use MYSQL statement across the table query cannot export all records of the solution
mysql index notes
How to automatically export or capture abnormal login ip and logs in elastic to the database?
Explain详解与实践
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
文件内容的操作