Javascript 如何避免在主机上使用脚本标记库cdn链接origin index.html
我有多个环境,如开发、预生产、生产,所以根据我需要访问库的环境 只有一个是关于环境的 因为我需要在加载应用程序时根据主机来源更改id值Javascript 如何避免在主机上使用脚本标记库cdn链接origin index.html,javascript,Javascript,我有多个环境,如开发、预生产、生产,所以根据我需要访问库的环境 只有一个是关于环境的 因为我需要在加载应用程序时根据主机来源更改id值 <script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
如果我在index.html中提到了3个脚本libs,那么函数gtag()将调用哪个lib数据我有多个环境,如开发、预生产、生产,因此根据环境,我需要使用脚本CDN链接放置不同ID 因此,与其隐藏lib,不如使用get
window.location.host创建新的脚本CDN链接代码>
var host = window.location.host;
var url;
if (host.includes('dev-something.com')) {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
} else if (host.includes('preprod-something.com')) {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
} else if (host.includes('something.com')) {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
} else {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
}
var head = document.getElementsByTagName('head')[0];
var theScript = document.createElement('script');
theScript.type = 'text/javascript';
theScript.src = url;
head.appendChild(theScript);
因此,它在加载我的索引文件时动态创建具有所需ID的CDN链接有没有想过使用webpack?
var host = window.location.host;
var url;
if (host.includes('dev-something.com')) {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
} else if (host.includes('preprod-something.com')) {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
} else if (host.includes('something.com')) {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
} else {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
}
var head = document.getElementsByTagName('head')[0];
var theScript = document.createElement('script');
theScript.type = 'text/javascript';
theScript.src = url;
head.appendChild(theScript);