English [en]
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
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.
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.
*
*/
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 (
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: 2012/05/31 20:33:03 $
