0
votes

How to include jquery libray src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" in an xternal .js file? Also how to include .css file in .js file?

2
Hi Jaideep, the question seems to be unclear. Can you post examples of what you have tried so far / or what you are trying to achieve? - dubes
I just want to include jquery lib in javascript file, because I want to write jquery code in javascript. Also i want to apply external css to divs that i create in javascript file - Jaideep
Go checkout this thread you will find the answer stackoverflow.com/questions/18261214/… - Danyal Fayyaz
How to apply external css to divs created in javascript file? - Jaideep

2 Answers

2
votes
var x = document.createElement('script');
x.src = 'http://example.com/test.js';
document.getElementsByTagName("head")[0].appendChild(x);
0
votes

You can only add JS files in HTML files. When you run a JS App open a website you're viewing the HTML file which has the JS files included.

If you're using jQuery you can add a JS file to HTML using :

 $.getScript(url, successCallback);

And here's' an async Vanilla JS function

function loadScript(src, callback)
{
  var s,
      r,
      t;
  r = false;
  s = document.createElement('script');
  s.type = 'text/javascript';
  s.src = src;
  s.onload = s.onreadystatechange = function() {
    //console.log( this.readyState ); //uncomment this line to see which ready states are called.
    if ( !r && (!this.readyState || this.readyState == 'complete') )
    {
      r = true;
      callback();
    }
  };
  t = document.getElementsByTagName('script')[0];
  t.parentNode.insertBefore(s, t);
}

Or just write all your code in one file or use build systems to concat and minify your files for production.