How to add jQuery in JS file

后端 未结 19 704
星月不相逢
星月不相逢 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:07

    Dynamic adding jQuery, CSS from js file. When we added onload function to body we can use jQuery to create page from js file.

    init();
    
    function init()
    {
    	addJQuery();
    	addBodyAndOnLoadScript();
    	addCSS();
    }
    
    function addJQuery()
    {
    	var head = document.getElementsByTagName( 'head' )[0];
    	var scriptjQuery = document.createElement( 'script' );
    	scriptjQuery.type = 'text/javascript';
    	scriptjQuery.id = 'jQuery'
    	scriptjQuery.src = 'https://code.jquery.com/jquery-3.4.1.min.js';
    	var script = document.getElementsByTagName( 'script' )[0];
    	head.insertBefore(scriptjQuery, script);
    }
    
    function addBodyAndOnLoadScript()
    {
    	var body = document.createElement('body')
    	body.onload = 
    	function()
    	{
    		onloadFunction();
    	};
    }
    
    function addCSS()
    {
    	var head = document.getElementsByTagName( 'head' )[0];
    	var linkCss = document.createElement( 'link' );
    	linkCss.rel = 'stylesheet';
    	linkCss.href = 'E:/Temporary_files/temp_css.css';
    	head.appendChild( linkCss );
    }
    
    function onloadFunction()
    {
    	var body = $( 'body' );
    	body.append('Hello world');
    }
    html 
    {
    	background-color: #f5f5dc;
    }
    
    
    	
    		
    		Temp Study HTML Page
    		
    	
    	
    

提交回复
热议问题