Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react load script after render

const loadScript = (url) => new Promise((resolve, reject) => {
  let ready = false;
  if (!document) {
    reject(new Error('Document was not defined'));
  }
  const tag = document.getElementsByTagName('script')[0];
  const script = document.createElement('script');

  script.type = 'text/javascript';
  script.src = url;
  script.async = true;
  script.onreadystatechange = () => {
    if (!ready && (!this.readyState || this.readyState === 'complete')) {
      ready = true;
      resolve(script);
    }
  };
  script.onload = script.onreadystatechange;

  script.onerror = (msg) => {
    console.log(msg);
    reject(new Error('Error loading script.'));
  };

  script.onabort = (msg) => {
    console.log(msg);
    reject(new Error('Script loading aboirted.'));
  };

  if (tag.parentNode != null) {
    tag.parentNode.insertBefore(script, tag);
  }
});


export default loadScript;
Comment

PREVIOUS NEXT
Code Example
Javascript :: validate email or phone js 
Javascript :: three dots in js 
Javascript :: javascript array read object value in array 
Javascript :: javascript select function 
Javascript :: javascript rect 
Javascript :: expres body parser 
Javascript :: all redux reuired packages 
Javascript :: get only string from html description javascript 
Javascript :: application pool angular 8 
Javascript :: set body id js 
Javascript :: javascript sort array of objects by value of key in object 
Javascript :: jquery attribute 
Javascript :: react native gif dont work 
Javascript :: storybook react router 
Javascript :: node.js error handling process 
Javascript :: javascript date parse yyyy-mm-dd 
Javascript :: js socket.emit 
Javascript :: instalar bootstrap en react 
Javascript :: factorial js 
Javascript :: Reduce array to a single string using reduce 
Javascript :: Material-ui Accessible icon 
Javascript :: js how to calculate factorial 
Javascript :: change terminal shortcut vscode 
Javascript :: sweetalert question 
Javascript :: Appending the option element using jquery each function 
Javascript :: javascript create json object from array 
Javascript :: jest tranform image 
Javascript :: discord button 
Javascript :: javascript valueOf() Method 
Javascript :: node schedule every minute 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =