提问人:Stellan Coder 提问时间:1/1/2022 最后编辑:Stellan Coder 更新时间:1/1/2022 访问量:356
在 JS 文件中包含 Javascript
Include Javascript inside JS file
问:
我们如何在文件中包含 javascript 链接? 文件支持包括、要求或导入,如 CSS 或 PHP?.js
.js
我的代码 :
// Global site tag (gtag.js) - Google Analytics
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://www.googletagmanager.com/gtag/js?id=UA-51600743-15';
document.body.appendChild(script);
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-51600743-15');
// Microsoft Clarity
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "9zvbjor3bn");
//Start of Tawk.to Script
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='https://embed.tawk.to/61d019a4c82c976b71c45176/1foae8bkm';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();
我正在尝试将所有跟踪代码添加到一个文件中,并将其添加到我的网站,以便将来易于维护,因此每当跟踪代码需要任何更改时,我都不需要在每个页面中进行编辑。analytic.js
在js文件中包含另一个js文件的正确方法是什么?
async src="https://www.googletagmanager.com/gtag/js?id=UA-51600743-15";
//OR
require("https://www.googletagmanager.com/gtag/js?id=UA-51600743-15"};
//OR
import from "https://www.googletagmanager.com/gtag/js?id=UA-51600743-15";
//Or any other way?
答:
1赞
Rajeev Ranjan Sharma
1/1/2022
#1
指南 : https://www.geeksforgeeks.org/how-to-include-a-javascript-file-in-another-javascript-file/
溶液:
function include(file) {
var script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.defer = true;
document.getElementsByTagName('head').item(0).appendChild(script);
}
/* Include Many js files */
include('https://example.com/example.js');
评论