从JavaScript函数有条件地调用脚本标记?
我使用Angular,需要从另一个脚本标记中的函数调用第三方脚本 例如: 在index.html中从JavaScript函数有条件地调用脚本标记?,javascript,html,angular,Javascript,Html,Angular,我使用Angular,需要从另一个脚本标记中的函数调用第三方脚本 例如: 在index.html中 let showOnHomePage = () => { if (window.location.href === 'http://localhost:4100/') { ** I'm trying to run the scripts below conditionally. If taken out of the function the below scripts
let showOnHomePage = () => {
if (window.location.href === 'http://localhost:4100/') {
** I'm trying to run the scripts below conditionally. If taken out of the function the below scripts work. **
<script> var _ctct_m = "my_numberic_key would go here"; </script>
<script id="newScript" src="https://thewebsite.min.js" async defer></script>
}
}
showOnHomePage();
</script>```
让showOnHomePage=()=>{
如果(window.location.href=='http://localhost:4100/') {
**我正在尝试有条件地运行下面的脚本。如果从函数中删除,下面的脚本将正常工作**
var\u ctct\u m=“我的数字键会在这里”;
}
}
showOnHomePage();
```
您可以从代码创建元素,包括script
标记:
function log(){console.log(stuff);}
函数脚本(){
设s=document.createElement(“脚本”);
s、 innerText=“let stuff=10;”;
文件。正文。附录子项;
}
Log
脚本
这是否回答了您的问题?谢谢你的回复。这会下载脚本,但它不会运行。@mwave317在不使用延迟的情况下尝试一下,因为它在页面加载时有意义,并且可能会在以后导致问题<代码>异步
感觉不那么“危险”,但您也可以尝试删除它。谢谢您的帮助。我让它与异步和延迟一起工作。问题是它需要这个var _ctct_m=“我的数字键会在这里”。在包含createElement(“脚本”)的函数的脚本标记上方;