0

I have an external js file which works fine only if I imported the jQuery library in my aspx file but when I drag and drop the jQuery library directly to the external js file, my external js doesn't work.

What is the proper way to import the jQuery on external js file or should the jQuery library be imported only in aspx page? Please help me understand.

Below is my code

    /// <reference path="Scripts/libframeworks/jQuery/jQuery-2.1.4.min.js" />
    $(function () {
      $("#btnSave").mouseover(function () {
      $(this).css("background-color", "Red");
      }).mouseleave(function () {
    $(this).css("background-color", "Blue");

});

});

If I remove this

/// <reference path="Scripts/libframeworks/jQuery/jQuery-2.1.4.min.js" />

and import it directly to the aspx page this way

     <script src="Scripts/libframeworks/jQuery/jQuery-2.1.4.min.js" type="text/javascript"></script>

my external js works fine. But I want to import jQuery library on my external js.

Francis Saul
  • 728
  • 2
  • 16
  • 37
  • 1
    Putting your jQuery reference one time in the aspx page allows it to be shared by all other scripts that might potentially need it. Burying a reference to it within another JS file is cumbersome and seems like a maintenance nightmare to me (I shouldn't have to hunt through files to find where jQuery came from). IMO your best option is to reference it manually outside of your file. If you're building a library, stating that it depends on jQuery should be enough for most developers. – Cᴏʀʏ Jan 20 '16 at 03:29
  • I got your point. thanks. But do you know how to import a library in external js? let's say I need a certain library only for this external js and doesn't want others to have reference on it. – Francis Saul Jan 20 '16 at 03:31
  • 1
    I think you're going to want to read [Include a JavaScript file in another JavaScript file?](http://stackoverflow.com/questions/950087/include-a-javascript-file-in-another-javascript-file) – Cᴏʀʏ Jan 20 '16 at 03:33

0 Answers0