Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * Loads a JavaScript file from the given URL and executes it.
  3. *
  4. * @param {string} url Address of the .js file to load
  5. * @param {function} callback Method to invoke when the script
  6. * has loaded and executed
  7. */
  8. export const loadScript = ( url, callback ) => {
  9. const script = document.createElement( 'script' );
  10. script.type = 'text/javascript';
  11. script.async = false;
  12. script.defer = false;
  13. script.src = url;
  14. if( typeof callback === 'function' ) {
  15. // Success callback
  16. script.onload = script.onreadystatechange = event => {
  17. if( event.type === 'load' || /loaded|complete/.test( script.readyState ) ) {
  18. // Kill event listeners
  19. script.onload = script.onreadystatechange = script.onerror = null;
  20. callback();
  21. }
  22. };
  23. // Error callback
  24. script.onerror = err => {
  25. // Kill event listeners
  26. script.onload = script.onreadystatechange = script.onerror = null;
  27. callback( new Error( 'Failed loading script: ' + script.src + '\n' + err ) );
  28. };
  29. }
  30. // Append the script at the end of <head>
  31. const head = document.querySelector( 'head' );
  32. head.insertBefore( script, head.lastChild );
  33. }