How to load js and css on a page on click?

  • 0
    I have a calculator that opens in the fancybox modal window, connects a massive js script, please tell me how to make its style files and js loaded only when you click to launch the calculator? Now it is loaded simultaneously with the page and defer does not help. can ajax help?
    JavaScript Anonymous, Sep 16, 2020

  • 2 Answers
  • 0
    importJS = function( uri ) {
    let script = document.createElement('script');
    let head = document.getElementsByTagName('head')[0];
    script.type = 'text/javascript';
    script.src = uri;

    when needed:

    importJS ('')

  • 0
    To load and run arbitrary JavaScript code, just run:

    $. getScript ("/path/test.js");

    Inside the function, you can pass either a full link or a relative one.
    Gabriella Buck

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!