简体   繁体   中英

Keeping my jQuery code away from the html files

I am learning jQuery and have a created several plug ins. Unfortunately due to my company's coding practices they want all javascript code to be extract out to js files. This for me poses two challenges:

  1. Can i extract the actual $(document).ready(..) call to a js file? So far with my limited knowledge I have not figured if this at all possible. If not, I welcome any suggestions to make this cleaner and more acceptable way to include this code.
  2. There are too many javascript includes for each asp.net page header since I may be using several plugins. Is there a way to reduce the potential costly server trips that I would need to make each time I need these files?

Any suggestions, corrections are greatly appreciated

thanks

1. Absolutely.

Just add a script reference to your html like this:

<script type='text/javascript' src='js/yourfile.js'></script> 

Then just start your .js file with

jQuery(function() {
  foo;
  ...
  bar;
});

or any other shortcut ways of starting the jQuery code block.

2. You should run your scripts through something like Minify before sending them off to the user. This will combine the files and pack them in nicely, so that they take up less space.

Using $(document).ready () in an external javascript file is fine - it will work exactly the same :) In fact - not only will it work, but it is good practice as it helps to seperate the content (HTML) from the behaviour (Javascript).

In response to your section question - you can combine all of your plugins into a single javascript file and link to that one inside the <head>. You could also try minifying the scripts, although this is normally a bit overkill until the site goes live.

When I use jQuery, I normally use this kind of structure:

<html>
  <head>
    <!-- html tags such as title, link, meta etc -->
    <script type="text/javascript" src="/path/to/jquery.js"></script>
    <script type="text/javascript" src="/path/to/plugin.js"></script>
    <!-- more plugins included if required -->
  </head>
  <body>
    <!-- html here -->

    <!-- script is the last thing before the ending body tag (increases performance) -->
    <script type="text/javascript" src="/path/to/your_jQuery_code.js"></script>
  </body>
</html>

I think worrying about server trips for javascript includes is premature optimization. Do you have any evidence that these pages are loading slowly? The browser should be caching the javascript files.

If you do have evidence that this is a problem, you could

-combine the jquery code and any plugins into one file
-write an .net content handler to do this for you (probably overkill)

Then you can add a custom js file per page to handle page specific properties.

You can most definitely put your document.ready and all other JavaScript code in an external file.

Typically I have 2 calls - one for jQuery itself, and one minified global.js file that combines and minifies all of my individual files.

Personally, I like to use front end blender for this, but there are many other options available as well.

there's nothing wrong w/putting the document.ready call in an external file. in fact, it's what i do to separate my js from my html. if you're concerned about certain functions running on certain pages, you may sift through them with a

var path = window.location.pathname;
if (path == "/yourdir/yourpage.html") {
    //do something for this page only
}

or you can just include certain files only on certain pages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM