JavaScript best practices

Use single quotation mark ' for strings.

var myString = 'String content';

If you need two or more sets of quotation marks, use ' on the outside, and " on the inside

var searchResult = '<li><a href="Result_URL">Result</a></li>';

Prefix jQuery objects with the dollar sign ($) so they can be distinguished from other objects.

var $header = $('#header');

Instead of using the $ variable directly use a wrapper to inject the jQuery object and only use the dollar sign in the local scope.

// The dollar sign will be used only inside the anonymous function here.
(function ($) {
    // Notice the shorthand document.ready function. Always wrap your jQuery DOM-manipulation code in the document.ready!
    $(function() {
        alert('Document ready!');
    });
})(jQuery);

Try to avoid adding variables to the global scope. A handy way of exposing globals is to namespace them under jQuery as demonstrated with the following example:

(function ($) {
    $.extend(true, {
        myModule: {
            // Such deep nesting is not always necessary, the method could be on this level directly
            myClass: { // More of a "class" than a real class of course
                myMethod: function () {
                    alert('myMethod called!');
                }
            }
        }
    });

    // You can use the above like this:
    $.myModule.myClass.myMethod();
})(jQuery);

When you want to access resources under a given URL of the current web application (like fetching data from a web API endpoint) never hard-code the URL into yours scripts. URLs can change and may depend on the environment (a trivial example being the usage of ApplicationPath that e.g. could prefix URL's during local development but can be empty in the production environment).

Instead inject such information into your scripts from templates.

This page is part of the Orchard Dojo Library!

This page and many others here are part of the open-source, portable package of Orchard 1.x goodies: the Orchard Dojo Library for Orchard 1.x. The Library is freely accessible by anyone; just go to its repository to download its full content (you can even download the textual content of the Library as a big concatenated document in HTML), file issues or fork it! You can even edit it online on Bitbucket, just click on the Edit link in the upper right corner!

Note that the Library also has an Orchard Core version, check it out here!