admin管理员组文章数量:1195300
I have a script tag that has a skype web control cdn in it, the script tag has been inserted in the head of my index.html, now the script tag is called before the component I need it in has loaded, any ideas on how I can reference this script in my component?
I have a script tag that has a skype web control cdn in it, the script tag has been inserted in the head of my index.html, now the script tag is called before the component I need it in has loaded, any ideas on how I can reference this script in my component?
Share Improve this question edited Oct 25, 2017 at 5:07 Smokey Dawson asked Oct 25, 2017 at 3:46 Smokey DawsonSmokey Dawson 9,23021 gold badges85 silver badges161 bronze badges 02 Answers
Reset to default 22I've figured it out, what you need to do is dynamically create the script tag and then append it to the head of your document:
export class Component {
//...
loadScript() {
let node = document.createElement('script'); // creates the script tag
node.src = ''; // sets the source (insert url in between quotes)
node.type = 'text/javascript'; // set the script type
node.async = true; // makes script run asynchronously
node.charset = 'utf-8';
// append to head of document
document.getElementsByTagName('head')[0].appendChild(node);
}
ngOnInit{
loadScript();
}
script tag is called before the component I need it in has loaded, any ideas on how I can reference this script in my component
Most script tags export a global variable. E.g React
$
_
etc. Read the docs on the lib and use the global variable they export.
版权声明:本文标题:javascript - How to dynamically add scripts into the head tag when angular component has loaded - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738491570a2089736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论