I love jQuery but am running into a problem with larger site and multiple pages. My problem is that each page has unique requirements and I need to know the best way to tell jQuery which pages to activate certain things. For example, some forms need the Validator plug-in and some don’t, some tables use DataTables plug-in and some don’t, etc.
Now I guess I could build complex logic (switch statements) into my application JavaScript file that fire different actions depending on what page they are on, but that just seems smelly. What is the Best Practice here?
UPDATE: There have been lots of good ideas on this question but not quite what I’m looking for. Let me rephrase the question in a more general way.
Currently I am using Rails and its Prototype helpers to build my AJAX components, but I want to move to UJS. How do I tell jQuery which links/buttons to make AJAX and which to avoid? And, given that I can differentiate the that are supposed to have AJAX, how do I give each link its own parameters (method, update, etc.) like I could with the helpers?
I mean besides building a huge page of specific jQuery selectors targeting each individual link/button. 🙂
A good practice is to have code that is required by all pages in one file and to have specific javascript files for pages that require specific functionality. It sounds as though this is what you are doing anyway, so we have a good basis to build upon.
There are numerous ways in which you could build in what pages need what files, but remember that in normal circumstances, javascript files are cached by the browser such that those files need only downloading once.
In light of this comment
I suggest coming up with a convention by which to label elements common across pages that require certain jQuery plugins “attached” to them. For example, if you have a
<form>element on a number of different pages that requires avalidator()plugin, but there is more than one<form>element on any one particular page (and not all<form>elements should have thevalidator()plugin), then I suggest using a CSS class to distinguish the<form>elements that do need the plugin.That way, the plugin will be applied only to those
<form>elements matching the selector.EDIT:
I’m not sure how the helpers in rails work, but you can pass data to an event handler in jQuery using the
dataparameter of thebind()method for any data that is not directly part of an<a>element itself (such as an attribute likehref). If some of the links require AJAX, then it may make sense to label those with a CSS class and store the URL in thehreffor the element. Then in your jQuery code that can be used on any pages that have links that make requests through AJAX, you could do something likethe link will work as per a normal link when a user has JavaScript disabled, but will make an AJAX request for data when JavaScript is enabled. I’ve used a named function here,
ajaxRequestas it can make it easier to debug the script (and perhaps also reuse), but you could use an anonymous function if you prefer.