Advertisement
rodro1

Fix Nuxt/vue Script load problem

Mar 15th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import * as external from '~/components/helper/external.js'
  2. mounted(){
  3. external.initialize_body_script('/js/custom/main.js'); //
  4. },
  5.  
  6.  
  7.  
  8. // PROJECT/src/assets/external.js
  9. function head_script(src) {
  10. if(document.querySelector("script[src='" + src + "']")){ return; }
  11. let script = document.createElement('script');
  12. script.setAttribute('src', src);
  13. script.setAttribute('type', 'text/javascript');
  14. document.head.appendChild(script)
  15. }
  16.  
  17. function body_script(src, defer=false) {
  18. if(document.querySelector("script[src='" + src + "']")){ return; }
  19. let script = document.createElement('script');
  20. script.setAttribute('src', src);
  21. script.setAttribute('type', 'text/javascript');
  22. if(defer){
  23. script.setAttribute('defer', 'true');
  24. }
  25. document.body.appendChild(script)
  26. }
  27.  
  28. function initialize_body_script(src){
  29. del_script(src);
  30. body_script(src);
  31. }
  32.  
  33. function initialize_head_script(src){
  34. del_script(src);
  35. head_script(src);
  36. }
  37.  
  38.  
  39. function del_script(src) {
  40. let el = document.querySelector("script[src='" + src + "']");
  41. if(el){ el.remove(); }
  42. }
  43.  
  44.  
  45. function head_link(href) {
  46. if(document.querySelector("link[href='" + href + "']")){ return; }
  47. let link = document.createElement('link');
  48. link.setAttribute('href', href);
  49. link.setAttribute('rel', "stylesheet");
  50. link.setAttribute('type', "text/css");
  51. document.head.appendChild(link)
  52. }
  53.  
  54. function body_link(href) {
  55. if(document.querySelector("link[href='" + href + "']")){ return; }
  56. let link = document.createElement('link');
  57. link.setAttribute('href', href);
  58. link.setAttribute('rel', "stylesheet");
  59. link.setAttribute('type', "text/css");
  60. document.body.appendChild(link)
  61. }
  62.  
  63. function del_link(href) {
  64. let el = document.querySelector("link[href='" + href + "']");
  65. if(el){ el.remove(); }
  66. }
  67.  
  68. export {
  69. head_script,
  70. body_script,
  71. del_script,
  72. head_link,
  73. body_link,
  74. del_link,
  75. initialize_body_script,
  76. initialize_head_script,
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement