Selective use of Javascript and CSS

There might be some situations when you want to run javascript code on a single type of page. For example : you might want to include a script that runs only on Author pages. You can do it with the conditional tag is_author() and wp_enqueue_script() function. Usage is similar to the is_admin() above.

Likewise, with conditional tags you can selectively use Javascript and css in wordpress.

Notes and Troubleshooting

When you replace the jQuery that ships with wordpress, make sure to use noConflict mode. This allows simultaneous usage of other script libraries. More about this here.

When your script is not working, check whether it is loaded. You can use Firefox’s Firebug extension (or Chrome’s Inspect element so). Look under the “Net” Panel –> “JS” tab for any 404 not found error(Colored Red). If it is a 404, then check whether the path to the script is correct. Double check the variables and concatenation, if you use them. If not, there must be something wrong with the javascript code. Most common culprits are spelling errors and case-sensitivity.

Check with firebug for any duplicate scripts. Plugins may sometimes use a different version of the same library that you use.

It is recommended to load the scripts in the wp footer. You can do this with $in_footer (5th) argument of wp_enqueue_script.

Always perform these procedures on a locally installed wordpress(PC, Mac), or at least on an alternate wordpress installation.

Conclusion

The purpose of this guide is to provide organized information that is often vague and scattered through out the web. If you have any questions/ wish to correct me, or criticism which i really appreciate, Please do.

Beginners learning Javascript can make use of these books – JavaScript: The Definitive Guide by David flanagan, or JavaScript: The Good Parts by Douglas Crockford. While Complete knowledge of javascript is not needed nowadays, because of availablity of multiple javascript frameworks such as jQuery, prototype. But it does serve build a strong base for the frameworks.

Kavin Gray is an User interface designer/ Web developer. He loves open source, follows occam's razor and is known for his all-consuming passion for Gadgets, Technology and AP. Learn more about him here. You can follow him on - Facebook or Twitter, maybe!