Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何避免在主机上使用脚本标记库cdn链接origin index.html_Javascript - Fatal编程技术网

Javascript 如何避免在主机上使用脚本标记库cdn链接origin index.html

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

我有多个环境,如开发、预生产、生产,所以根据我需要访问库的环境 只有一个是关于环境的 因为我需要在加载应用程序时根据主机来源更改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>

<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);