10

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?

0

2 Answers 2

22

I'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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you confirm this is safe to use as we're manipulating the DOM?
@DragosStanciu yes this is safe to use
You may want to wait until the script fully loaded. See this
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.