0

I am planning on separating independent .js files and I wonder if I can use them together after that.

The problem might be that all of them have something similar to

$(function() { code here };

Is it safe to make jQuery to run multiple times, from different locations, this method?

Odys
  • 8,951
  • 10
  • 69
  • 111
  • Yes. It's not a problem at all. – James Allardice Sep 05 '11 at 13:13
  • Possibly related: *[What happens when you have two jQuery $(document).ready calls in two JavaScript files used on the same HTML page?](http://stackoverflow.com/questions/6435868/what-happens-when-you-have-two-jquery-document-ready-calls-in-two-javascript-f/6435978#6435978)* – jensgram Sep 05 '11 at 13:19

4 Answers4

1

If you are using many .js files you might want to use require.js (http://requirejs.org/). This framework will optimize the loading of multiple scripts.

bittersweetryan
  • 3,383
  • 5
  • 28
  • 42
0

Yep, that should be fine. And you can reuse document.ready() too (info from jQuery docs here).

Timbo
  • 4,505
  • 2
  • 26
  • 29
0

YES

I see no reason why wouldn't you be able to "run" jQuery multiple times.. It's function and function is about to be albe to be called multiple times

genesis
  • 50,477
  • 20
  • 96
  • 125
0

Yes. .ready(callback) is shorthand for .bind("ready", callback);. Event bind calls in jQuery let you add multiple handlers for a single event.

Dennis
  • 32,200
  • 11
  • 64
  • 79