【前端】脚本加载失败怎么办

DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>脚本加载失败怎么办title>
    <script>
        const domains = [
            '2345fadwfasdfada.com',
            'other-domain.com',
            'backup.com',
        ];
        const maxRetry = 3;
        const retryInfo = {};
        window.addEventListener('error', (e) => {
            // console.log('有错误');
            const tag = e.target;
            if (tag.tagName === 'SCRIPT' && !(e instanceof ErrorEvent)) {
                // console.log('script加载失败');
                const url = new URL(tag.url);
                if (!retryInfo(url.pathname)) {
                    retryInfo[url.pathname] = {
                        time: 0,
                        nextIndex: 0
                    }
                }
                const info = retryInfo[url.pathname];
                // console.log(info)
                const script = document.createElement('script');
                url.host = domains[info.nextIndex];
                // 阻塞页面后续的加载
                document.write(`
                    
                    

你可能感兴趣的:(JavaScript,前端,javascript,vue.js)