Tips and best practices for JavaScript files:
- Use wp_register_script() and wp_enqueue_script() to initialize your Javascript files. This ensures compatibility with other plugins and avoids conflicts.
- As with everything, prefix your script slugs when registering or enqueuing them.
- The jQuery version that is packaged with WordPress is in compatibility mode. This means that
jQuery()needs to be explicitly used, not the$()short form. - Avoid usage of scripts not hosted on WordPress.com. There is a comprehensive list of scripts included. All WordPress.com scripts are served by a CDN.
- If you need to register a script that is not part of WordPress, or your your theme, make sure to use a packed version if available and make sure that their servers are up for the traffic you will request from them. Fail gracefully.
- Adding a version to the script when enqueueing isn’t necessary. When we concatenate files, we append the filemtime of the last updated file as a cache buster. If the file isn’t concatenated for some reason, we just use the file’s mtime.