How to add jQuery in JS file

后端 未结 19 752
星月不相逢
星月不相逢 2020-12-04 05:47

I have some code specific to sorting tables. Since the code is common in most pages I want to make a JS file which will have the code and all the pages using it can referen

19条回答
  •  悲哀的现实
    2020-12-04 06:21

    /* Adding the script tag to the head as suggested before */
    
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = "http://code.jquery.com/jquery-2.2.1.min.js";
    
        // Then bind the event to the callback function.
        // There are several events for cross browser compatibility.
        script.onreadystatechange = handler;
        script.onload = handler;
    
        // Fire the loading
        head.appendChild(script);
    
        function handler(){
           console.log('jquery added :)');
        }
    

提交回复
热议问题