Setting Your JavaScript Free

by Loic Duros

This explains how to release your JavaScript as free software so that it respects your users' freedom, and how to clearly indicate this fact so that LibreJS will validate it as released properly.

For an explanation of why it is crucial to make your JavaScript free, see The JavaScript Trap.

Step 1: Identifying all JavaScript on your page

Before you can add proper license information to your JavaScript, you must identify all the JavaScript present on your page.

GNU LibreJS can list both the blocked and accepted JavaScript code on a page and make this step much easier. To use it, enable LibreJS in IceCat, Abrowser, Iceweasel, or Firefox, and visit your page. Clicking on the LibreJS widget icon (bottom right of the screen) will show you which JavaScript code is blocked by LibreJS and which code is accepted.

External JavaScript files

External JavaScript files are easy to identify because they use a <script> element with an src attribute:

<script src="myfile.js"></script>

JavaScript embedded in a <script> tag

Many times, JavaScript code is directly embedded in a <script> tag, e.g.:

<script>
var myString = "Hello World!";
alert(myString);
</script>

JavaScript in event handlers

JavaScript can also be added to event handlers: inside an HTML attribute that is triggered at a specific moment or in a specific state. For instance, the following code will execute JavaScript when clicking on the link:

 <a href="#" onclick="alert('Hello World!')">Say Hello</a>

and the following will be executed right after the page has loaded:

 <body onload="alert('Hello World!')">

LibreJS also lists these elements for you in the display panel.

JavaScript embedded dynamically

Some advanced JavaScript constructs load a file of JavaScript code whose name is computed at run time. If LibreJS encounters these constructs in properly licensed free JavaScript code, it checks the files that they try to load, and tells you their names and whether they are free.

Other advanced JavaScript constructs execute JavaScript code computed by the program.

Since it is impossible in general for a machine to understand what that program does, LibreJS cannot tell what that code is or whether it comes from, or whether it is free.

You should only worry about this sort of scripts after you have set proper licensing to all the other forms of JavaScript on your page and it is still not behaving as expected when running the page with LibreJS on. This usually means LibreJS is blocking further scripts that are loaded dynamically, and you need to add free license notices to them as well.

Step 2: Making decisions about your JavaScript

After you have a good idea of what JavaScript is present on your page, you should be able to divide all these scripts into three categories:

Scripts that are not yours, but are free

This is often the case with libraries, such as jQuery, Mootools, or Prototype, and the third-party plugins that are developed with it. LibreJS v4.7 can detect many free libraries by default, but there are too many plugins available for these libraries and you will still need to add license information to plugins. By looking at the source code or checking the upstream site, you should be able to tell what license is being used. Please ensure these licenses are free and GPL-compatible.

Scripts that are not yours and are not free

On many websites you can find third-party code that is not free. This is often the case with Analytics trackers, Social Media widgets, JavaScript code that displays advertising, third-party widgets to write comments on a given page.

Please take the time to request the provider of this script to release it under a free license. But unless they do so, freeing your site requires removing or replacing this JavaScript code.

Often you can find existing free replacements for the functionality of the nonfree script. We would like to help you; write to help-librejs@gnu.org.

In some cases (analytics and social media sites), what the code does is give information about your users to companies. That would be a bad thing for your site to do even if it were done with free software. The right way to do analytics is with free software such as Piwik. That way you don't give information about your site's visitors to anyone.

Scripts that are yours

If some of the JavaScript is yours, then you can decide to release it under a free license. We recommend you use the GNU General Public License version 3 or later. You can find a list of licenses detected as free by LibreJS in the project's documentation.

Step 3: Adding license information

Once you have removed nonfree JavaScript from your page and have a good idea of what JavaScript is free, you can go ahead and add proper license information to your page and your external scripts.

JavaScript embedded on your page and in event handlers. Method 1: adding license information for the entire page

The embedded JavaScript code can be read directly from the HTML source of the page. This excludes external scripts that are loaded with the src attribute. You must release all of this embedded code under a free license, using the stylized comment. The license comment should be the first piece of JavaScript code available on the page. To ensure this is the case, we recommend you place the comment in its own <script> tag, before all the other scripts, e.g.:

<!doctype html>
<html>
    <head>
        <title>My Page</title>

        <meta charset="UTF-8" />

        <script>
        /*    
        @licstart  The following is the entire license notice for the 
        JavaScript code in this page.

        Copyright (C) 2012  Loic J. Duros

        The JavaScript code in this page is free software: you can
        redistribute it and/or modify it under the terms of the GNU
        General Public License (GNU GPL) as published by the Free Software
        Foundation, either version 3 of the License, or (at your option)
        any later version.  The code is distributed WITHOUT ANY WARRANTY;
        without even the implied warranty of MERCHANTABILITY or FITNESS
        FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

        As additional permission under GNU GPL version 3 section 7, you
        may distribute non-source (e.g., minimized or compacted) forms of
        that code without the copy of the GNU GPL normally required by
        section 4, provided you include this license notice and a URL
        through which recipients can access the Corresponding Source.   


        @licend  The above is the entire license notice
        for the JavaScript code in this page.
        */
        </script>

        <script>
            // some JavaScript comes here and further down
            // the document.
        </script>

    </head>
    ...

You need not worry about whitespaces and new lines. The @licstart and @licend lines are needed so that it is explicit that all the JavaScript on the page is free.

As mentioned in the previous section, you can find a list of licenses detected as free by LibreJS in the project's documentation.

More information on the stylized comment is available in Appendix A of the JavaScript Trap.

After adding a license notice similar to the one above, all the JavaScript directly present on the page will be detected as free by LibreJS, and it will go on to analyze the external scripts individually.

LibreJS 5.0 introduced a new method to tag your JavaScript as free using special comments and magnet links pointing to the license files.

Instead of adding a license notice between @licstart and @licend, you simply add the following comments at the top and bottom of the script:

<script>
// @license [magnet-link] [human readable name of the license]
... [script is here] ...
// @license-end
</script>

You can also use the /* */ comment format:

<script>
/* @license [magnet-link] [human readable name of the license] */
... [script is here] ...
/* @license-end */
</script>

For a script released under the GPLv3 or later, this would become:

<script>
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
var myString = "Hello World!";
alert(myString);
// @license-end
</script>

After adding the correct license, this specific script will appear as free to LibreJS. Please note that the human readable string does not require a specific formatting, only the magnet link is checked. Please note that no code or comment should be added to the script after the // @license-end comment. However, whitespace is allowed.

List of magnet links for valid licenses

The following magnet links will be recognized as free licenses:

  • CC0:
    • magnet:?xt=urn:btih:90dc5c0be029de84e523b9b3922520e79e0e6f08&dn=cc0.txt
  • GPLv2.0 or later:
    • magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt
  • GPLv3 or later:
    • magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt
  • Apache 2.0:
    • magnet:?xt=urn:btih:8e4f440f4c65981c5bf93c76d35135ba5064d8b7&dn=apache-2.0.txt
  • LGPL v.21:
    • magnet:?xt=urn:btih:5de60da917303dbfad4f93fb1b985ced5a89eac2&dn=lgpl-2.1.txt
  • LGPL v3.0:
    • magnet:?xt=urn:btih:0ef1b8170b3b615170ff270def6427c317705f85&dn=lgpl-3.0.txt
  • AGPL v3.0:
    • magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt
  • BSD 3 Clause:
    • magnet:?xt=urn:btih:c80d50af7d3db9be66a4d0a86db0286e4fd33292&dn=bsd-3-clause.txt
  • MPL 2.0:
    • magnet:?xt=urn:btih:3877d6d54b3accd4bc32f8a48bf32ebc0901502a&dn=mpl-2.0.txt
  • Expat:
    • magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt
  • X11:
    • magnet:?xt=urn:btih:5305d91886084f776adcf57509a648432709a7c7&dn=x11.txt
  • XFree86:
    • magnet:?xt=urn:btih:12f2ec9e8de2a3b0002a33d518d6010cc8ab2ae9&dn=xfree86.txt
  • FreeBSD:
    • magnet:?xt=urn:btih:87f119ba0b429ba17a44b4bffcab33165ebdacc0&dn=freebsd.txt
  • ISC:
    • magnet:?xt=urn:btih:b8999bbaf509c08d127678643c515b9ab0836bae&dn=ISC.txt
  • Artistic 2.0:
    • magnet:?xt=urn:btih:54fd2283f9dbdf29466d2df1a98bf8f65cafe314&dn=artistic-2.0.txt
  • Public Domain:
    • magnet:?xt=urn:btih:e95b018ef3580986a04669f1b5879592219e2a7a&dn=public-domain.txt

Please note: When people speak of the "MIT license" they mean either the X11 license or the expat license. Please see which license the code uses, and label it accordingly.

External JavaScript files

The external JavaScript files that are added using the <script src=".."> attribute and the scripts that are embedded dynamically are analyzed individually by LibreJS. Each of them may have a different free license.

Stylized comment

For external scripts, you can use the same sort of stylized comments as the HTML page. If your JavaScript file is minified or obfuscated in any way, please remember to add a @source line with the location of a source file:


/**
 *
 * @source: http://www.lduros.net/some-javascript-source.js
 *
 * @licstart  The following is the entire license notice for the 
 *  JavaScript code in this page.
 *
 * Copyright (C) 2012  Loic J. Duros
 *
 *
 * The JavaScript code in this page is free software: you can
 * redistribute it and/or modify it under the terms of the GNU
 * General Public License (GNU GPL) as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * any later version.  The code is distributed WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.
 *
 * As additional permission under GNU GPL version 3 section 7, you
 * may distribute non-source (e.g., minimized or compacted) forms of
 * that code without the copy of the GNU GPL normally required by
 * section 4, provided you include this license notice and a URL
 * through which recipients can access the Corresponding Source.
 *
 * @licend  The above is the entire license notice
 * for the JavaScript code in this page.
 *
 */

You can also use the // @license [magnet-link] and // @license-end notation for external scripts in the same way as scripts directly embedded on a page. Please read Method 2: Specific license information for each script for information on how to do this.

JavaScript Web Labels

If bandwidth is an issue for you and your visitors, then you can opt for the JavaScript Web Labels. In order to use this method, create an HTML page in your site. For instance, you could write the file in about/javascript.html on your website. This new page should have a simple HTML table inside of it. Other HTML elements such as your site's template can be present too. The information table should look something like this:

<table id="jslicense-labels1">

<tr>
<td><a href="/js/jquery-1.7.min.js">jquery-1.7.min.js</a></td>

<td><a href="http://www.jclark.com/xml/copying.txt">Expat</a></td>

<td><a href="/js/jquery-1.7.tar.gz">jquery-1.7.tar.gz</a></td>
</tr>

</table>

It should have the id of 'jslicense-labels1'. For each external script, you must add a table row (). In each row, the first column cell () must contain a link to the JavaScript file. The second row must contain the name of the license with the canonical URL to the license. Finally the third column must contain a link to the source.

LibreJS identifies the licenses for these scripts using the canonical URL in the second column. A list of the licenses currently detected along with their canonical URL is available in the GNU LibreJS documentation.

Finally, you must add a link to the new page from all the pages loading these files. Don't forget to add a "rel" attribute with "jslicense" as its value:

<a href="/about/javascript" rel="jslicense">JavaScript license information</a>

More information on the JavaScript Web Labels is available here and in the JavaScript Web Labels rationale document.

If you need any assistance releasing your JavaScript under free licenses, please contact help-librejs@gnu.org.

Updated: $Date: 2014/07/07 11:31:44 $