How to add jQuery in JS file

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

    The problem is you're using </script> within the script, which is ending the script tag. Try this:

    document.writeln('<script src="/javascripts/jquery.js" type="text/javascript"></sc'+'ript>');
    document.writeln('<script type="text/javascript" src="/javascripts/jquery.tablesorter.js"></sc'+'ript>');
    
    0 讨论(0)
  • 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('<strong>Hello world</strong>');
    }
    html 
    {
    	background-color: #f5f5dc;
    }
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="UTF-8">
    		<title>Temp Study HTML Page</title>
    		<script type="text/javascript" src="E:\Temporary_files\temp_script.js"></script>
    	</head>
    	<body></body>
    </html>

    0 讨论(0)
  • 2020-12-04 06:07
    var jQueryScript = document.createElement('script');  
    jQueryScript.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js');
    document.head.appendChild(jQueryScript);
    
    0 讨论(0)
  • 2020-12-04 06:08

    Theres a plugin for jquery where you can just include the files you need into some other js file, here is the link for it http://tobiasz123.wordpress.com/2007/08/01/include-script-inclusion-jquery-plugin/.

    Also this document.write line will write the script tags in the html not in your js file.

    So I hope this could help you out, a little with your problem

    0 讨论(0)
  • 2020-12-04 06:09

    After lots of research, I solve this issue with hint from ofir_aghai answer about script load event.

    Basically we need to use $ for jQuery code, but we can't use it till jQuery is loaded. I used document.createElement() to add a script for jQuery, but the issue is that it takes time to load while the next statement in JavaScript using $ fails. So, I used the below solution.

    myscript.js is having code which uses jQuery main.js is used to load both jquery.min.js and myscript.js files making sure that jQuery is loaded.

    main.js code

    window.load = loadJQueryFile();
    var heads = document.getElementsByTagName('head');
    
    function loadJQueryFile(){
        var jqueryScript=document.createElement('script');
        jqueryScript.setAttribute("type","text/javascript");
        jqueryScript.setAttribute("src", "/js/jquery.min.js");
    
        jqueryScript.onreadystatechange = handler;
        jqueryScript.onload = handler;  
        heads[0].appendChild(jqueryScript);
    }
    
    function handler(){
        var myScriptFile=document.createElement('script');
        myScriptFile.setAttribute("type","text/javascript");
        myScriptFile.setAttribute("src", "myscript.js");
        heads[0].appendChild(myScriptFile);
     }
    

    This way it worked. Using loadJQueryFile() from myscript.js didn't work. It immediately goes to the next statement which uses $.

    0 讨论(0)
  • 2020-12-04 06:11

    Why are you using Javascript to write the script tags? Simply add the script tags to your head section. So your document will look something like this:

    <html>
      <head>
        <!-- Whatever you want here -->
        <script src="/javascripts/jquery.js" type="text/javascript"></script>
        <script src="/javascripts/jquery.tablesorter.js" type="text/javascript"></script>
      </head>
      <body>
        The contents of the page.
      </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题