问题
需要判断url是否可用,再去执行其他操作,代码如下:
const loadScript = (url) => {
return new Promise((resolve, reject) => {
const script = document.createElement('script')
script.onload = () => resolve(true)
script.onerror = () => resolve(false)
script.src = url
const head = document.head || document.getElementsByTagName('head')[0];
(document.body || head).appendChild(script)
})
}
const checkUrl = async() => {
const url = 'xxxx';
const flag = await loadScript(url)
if (flag) {
// do something...
}
}
标签:head,resolve,const,script,url,可用,js,document
From: https://www.cnblogs.com/ZerlinM/p/17814831.html