How to Include jQuery and Plugins in a Separate JavaScript File?

How do you add jQuery in a JS file?

I have some code specific to sorting tables, and since the code is common across multiple pages, I want to create a separate JS file for it. All pages can reference this file.

The issue is: how to use jQuery in a JavaScript file and include the table sorter plugin as well.

I tried this approach:

You can use document.writeln and add your source within. eg: src: /javascripts/jquery.js" type="text/javascript"

To load table sorter you can use the below: document.writeln add your script src like this : /javascripts/jquery.tablesorter.js

But it doesn’t seem to work.

What is the best way to properly include jQuery and plugins in a separate JavaScript file?

Reference jQuery in HTML before your JS file Instead of dynamically inserting jQuery within your JavaScript, include the jQuery and table sorter plugin directly in the HTML file before your custom JavaScript file. This ensures that jQuery is loaded before your script is executed.

You load the script and add your source file: for custom, you can use this src="/javascripts/sorting.js" ( sorting.js is my file name you can add yours).

This guarantees that jQuery and the plugin are available when your sorting.js runs.