Difference between revisions of "Jmol JavaScript Object"
AngelHerraez (talk | contribs) (updating and rearranging info on JSO/JSmol) |
AngelHerraez (talk | contribs) (installation instructions moved to separate page and updated to Jmol 16) |
||
(160 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{Lang:Jmol JavaScript Object}} | |
− | + | == JSmol, the Jmol JavaScript Object == | |
− | + | <div style="float:right; margin-left:1em;"> | |
+ | __TOC__ | ||
+ | </div> | ||
+ | <div style="float:left;"> | ||
+ | [[Image:JSmol_logo13.png]] | ||
+ | </div> | ||
− | + | The heart of JSmol is the Jmol JavaScript object, (<code>window.Jmol</code>), which includes a set of JavaScript functions and utilities. The initial Jmol object was developed by Bob Hanson and Paul Pillot in early 2012. It was folded into '''JSmol''' later that year. The library {{file| JSmol.min.js}} provides this object and supersedes the Jmol.js JavaScript library formerly used exclusively with the Jmol Java applet, allowing a cleaner, more efficient way to interact with Jmol on a web page, and abstracting the visualization of a molecular model so that the Java applet can be seamlessly and selectively replaced by a non-Java HTML5 canvas element (which might not even be Jmol). | |
− | + | Using '''JSmol''', a web page developer can target most web browsers that no longer support Java applets, using '''a Jmol applet surrogate''', while still implementing the Jmol Java applet on compatible platforms. | |
− | + | '''JSmol''' also integrates facilities for direct, behind-the-scenes access to public databases such as the [https://rcsb.org RCSB PDB database], the [https://cactus.nci.nih.gov/ National Cancer Institute CACTVS server], and [https://pubchem.ncbi.nlm.nih.gov/ PubChem]. | |
− | ''' | + | In addition, '''JSmol''' allows easy interaction with |
+ | * the '''JSpecView HTML5 object''', allowing one to tie 3D models to IR, NMR, UV/VIS, GC, and GC/MS spectra (see [[Jmol_JavaScript_Object/JSV|more details]]) | ||
+ | * the '''JSME HTML5 object''', so 3D models can be generated by a page visitor using simple chemical 2D drawing (see [[Jmol_JavaScript_Object/JME|more details]]) | ||
− | + | '''JSmol''' is fully compatible with [http://jquery.com jQuery] 1.9 or 1.10. (jQuery 2.0 does not and for reasons of principle reportedly will never support MSIE running locally using file://, so if you do not need to run your page in MSIE in a local environment, that should be fine. There is a fix for this, however, if you must do that. In that case you must hack jQuery a bit.) The library has been W3C and XHTML validated. | |
− | |||
− | |||
− | + | ===Main features of JSmol === | |
− | |||
− | === | ||
<table class="wikitable"> | <table class="wikitable"> | ||
<tr> | <tr> | ||
− | <th> | + | <th>Non-Java options</th><td>Options for HTML5-only and optionally Java and HTML5/WebGL. Includes a variety of options, such as initial "deferred-applet" mode, where an initial image is displayed, with a click on the image or link on the page initiating applet/canvas 3D modeling, and "image+loading" mode, in which case the 3D model is loading behind the scenes while an initial image is displayed.</td> |
</tr> | </tr> | ||
<tr> | <tr> | ||
− | <th> | + | <th>Library files</th> |
+ | <td>Requires at a minimum {{file|JSmol.min.js}} as well as {{folder|j2s}}; for internationalized interface, also {{folder|idioma}}. Also optionally {{folder|java}}.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
− | <th> | + | <th>JavaScript objects</th> |
− | <td> | + | <td>Creates a single JavaScript object, <code>Jmol</code>, which includes a set of functions and internal objects such as <code>Jmol._Applet</code>, <code>Jmol._Image</code>, and <code>Jmol.controls</code>.</td> |
− | < | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
− | <th>JavaScript | + | <th>JavaScript prototypes</th> |
− | <td> | + | <td>The object you create using <code>Jmol.getApplet()</code> or <code>Jmol.getAppletHtml()</code> is a JavaScript object that is a subclass of <code>Jmol._Applet</code>. When you use <code>Jmol.getApplet()</code>, you get a reference to a JavaScript object, not the applet/canvas itself. The applet or canvas is wrapped in a set of <code>div</code> elements, allowing a richer diversity of options.</td> |
− | |||
</tr> | </tr> | ||
<tr> | <tr> | ||
− | <th> | + | <th>AJAX</th> |
− | <td> | + | <td>JSmol includes methods to easily access cross-platform resources using AJAX provided by jQuery.</td> |
− | |||
</tr> | </tr> | ||
<tr> | <tr> | ||
− | <th> | + | <th>REST services</th> |
− | <td> | + | <td>JSmol lets you access keyword search results from RCSB -- for example, a list of all structures that reference ''caffeine''.</td> |
− | |||
</tr> | </tr> | ||
<tr> | <tr> | ||
<th>Scripting</th> | <th>Scripting</th> | ||
− | <td> | + | <td>JSmol provides the same full complement of scripting that Jmol offers. JSmol accepts script commands immediately, before or during applet/canvas creation on the page, caching them until Jmol is ready to accept them.</td> |
− | |||
</tr> | </tr> | ||
</table> | </table> | ||
== JSmol == | == JSmol == | ||
− | . | + | <div style="float:left;"> |
+ | [[Image:JSmol_logo13.png]] | ||
+ | </div> | ||
− | + | '''JSmol''' is the name for the HTML5 canvas version of the former Jmol applet. '''JSmol''' opens up the use of Jmol in PC, Mac, and Linux systems, tablets and phones (both iOS and Android) without the need for Java. No hardware-based graphics acceleration is used, allowing '''JSmol''' to run in any web browser that supports HTML5 standards. JSmol runs entirely in the client, needing no server technologies for most of its operation. (Reading binary files in some browsers and saving images and Jmol states in all browsers do require a server-side PHP script, commonly provided as {{file|php/jsmol.php}} –read details below.) | |
− | + | '''JSmol''' was developed by Bob Hanson, Zhou Renjian, and Takanori Nakane. | |
− | + | '''JSmol''' allows rendering, scripting and interaction with the models just as Jmol does, since the source code is shared by both. Note that JSmol is not a different program than Jmol: it is Jmol, just compiled into JavaScript instead of Java (thanks to the [http://java2script.sourceforge.net/ Java2Script] software). | |
− | JSmol | + | ===Current limitations of JSmol === |
+ | * Using local files (i.e. not in a web server) is not supported on some browsers due to their security policies: | ||
+ | ** Opera allows no access to local files. | ||
+ | ** MSIE allows access to local files only if they are not binary. | ||
+ | ** Chrome allows access only when Chrome has been started using a special command-line flag (<code>chrome.exe --allow-file-access-from-files</code>). | ||
+ | ** Firefox allows access only after setting a special <code>about:config</code> flag (<code>security.fileuri.strict_origin_policy = false</code>). | ||
+ | * Opening binary files (e.g Spartan files, gzipped files, {{file|pngj}} files): they can be read but must be identified as such in their file name (see below). | ||
+ | * Writing of {{file|jpg}}, {{file|png}} and {{file|pngj}} (png+zip) data is fully supported, but delivering it to a user may require a server-side piece. (See also [[Recycling_Corner#Exporting_an_image_from_the_applet|Exporting an image]] for alternatives). | ||
+ | * The Jmol <code>prompt</code> command does not allow more than a simple JavaScript-like response in the HTML5 version. | ||
+ | * The [[Jmol_JavaScript_Object/WebGL|WebGL modality]] of JSmol has not been fully developed and feature support is limited. | ||
− | + | === Reading binary files === | |
+ | Jmol (Java) can open a file and then determine whether the file is binary or not. JSmol (JavaScript) is different, because the AJAX transport mechanism is different for binary and nonbinary files. Thus, JSmol must determine whether a file is binary or not ''prior'' to its loading. | ||
− | + | ==== File names ==== | |
− | + | JSmol will determine whether a file is binary or not —before loading it— by inspection of the file name. JSmol will switch to binary mode for files with any of the following in their file name: | |
− | + | {{file|.bin}} | |
− | + | {{file|.gz}} | |
− | + | {{file|.zip}} | |
− | + | {{file|.jpg}} | |
− | + | {{file|.png}} | |
− | + | {{file|.jmol}} | |
− | + | {{file|.smol}} | |
− | + | {{file|.spartan}} | |
− | + | {{file|.mrc}} | |
− | + | {{file|.pse}} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | These "extensions" can appear anywhere in a file name to trigger the binary access mode. So, for example, if you rename any file to include <code>.bin</code> anywhere in its name, that will instruct JSmol to read it as a binary file. | |
− | |||
− | |||
− | |||
− | + | :''Advanced'': If you need a different extension to be read as binary, and cannot change the file names to include one of the default extensions, it is possible to extend the set using this code in your JavaScript after the Jmol object has being initialized: <code>Jmol._binaryTypes.push('.myExtension');</code> | |
− | === | + | ==== Browser behaviour ==== |
+ | For binary file reading to be compatible with Chrome and MSIE, you will need to have a base64 server-side piece that will convert the binary data to <code>BASE64</code> format. This is because only Firefox allows clean (reliable) synchronous binary file transfer. (And, so far, we have not figured out how to move all the file loading in Jmol to a fully asynchronous mode.) | ||
− | + | If your page visitors need to read binary files, place a copy of the {{file|jsmol.php}} file that is included in the JSmol distribution (see below) on your server and point to it using the [[Jmol_JavaScript_Object/Info#Files_and_paths|'''serverURL''' item of the Info array]]. | |
+ | As an example, the [{{StOlaf}}jsmol/jsmol.htm demo pages] use this approach. | ||
− | + | For that to work, your server must support PHP. Otherwise, you might "borrow" by pointing to the {{file|jsmol.php}} file in another server that can run PHP (please, ask the owner of that server). | |
− | |||
− | |||
− | + | In Chrome and also other browsers, you may overcome problems with loading '''''local''''' binary files by running pages through a local web server. | |
− | + | === Converting legacy web pages to JSmol === | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | It is not hard to convert pages that used the Jmol Applet and the <code>Jmol.js</code> library to use the HTML5 versions of JSmol. See [[Jmol JavaScript Object/Legacy|more details]]. | ||
+ | ==Installation== | ||
+ | See [[Jmol JavaScript Object/Installation]] | ||
− | ==== | + | ==Initialization== |
− | + | === Browser issues === | |
− | + | JSmol makes strong use of the HTML5 features. Therefore, it is only compatible with modern web browsers. Specifically, ''Internet Explorer must be version 9 or higher''. | |
− | |||
− | |||
− | + | In addition, it is important to use a doctype in the header of the html page. The recommended doctype is the simple one, associated to HTML5 standard, as the very first line of your html document. | |
− | + | Also, for full compatibility, particularly for the localization (language translations) of the JSmol pop-up menu, you should declare the charset as UTF-8 and save the html document (and all accessory files) using UTF-8 encoding (usually without [[wikipedia:Byte_order_mark#UTF-8|BOM]], but this needs further confirmation; recent versions of Firefox seem to have problems with non-BOM UTF-8 files, and UTF-8 with BOM looks like a better solution). | |
− | < | + | Therefore your html documents should start as: |
− | + | <!DOCTYPE HTML> | |
− | < | + | <html> |
− | + | <head> | |
+ | <meta charset="utf-8"> | ||
− | + | === Loading the library === | |
− | <code> | + | The web page should have the following in the <code>head</code> section (pointing to appropriate paths if not the same folder as the web page as shown here): |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | <code><script type="text/javascript" src="JSmol.min.js"></script></code> | |
− | + | Please note that there may be restrictions on the folder locations where you can put files for the libraries (js), the page (html), the models and scripts. This is imposed by browser security policies (related to both Java security and AJAX calls in JavaScript) and usually applies only to situations where you are using the pages from local disk (as opposed to a web server). | |
− | + | : Successful file access by JSmol depends upon whether the page is loaded using "file:" or "http:" | |
+ | :: <code>http:</code> all files should be on the host server or on a server set up to deliver cross-domain AJAX using the "Access-Control-Allow-Origin: *" header. | ||
+ | :: <code>file:</code> all files should be on the local machine within or under the folder containing the {{file|.html}} page. | ||
+ | : ''All this needs confirmation''. See also [[Troubleshooting/Applet#Java_security_errors|Java security policy]] which applies to the Java applet. | ||
− | === | + | ==== Lightweight JSmol ==== |
− | + | There is an alternative version of Jmol object that, using the HTML5 canvas like JSmol, loads very fast while offering minimal functionality (just a simplified ball and stick rendering, no scripting). It is specially aimed at smartphones and such systems with limited resources. | |
− | + | For more details on how to use this, see [[Lightweight JSmol]]. | |
− | |||
− | + | ==== Optional components ==== | |
+ | * [[Jmol_JavaScript_Object/JME|Integration with JSME]], an editor to draw 2D chemical structures (Peter Ertl's JavaScript Molecular Editor) | ||
+ | * [[Jmol_JavaScript_Object/JSV|Integration with JSpecView]], a viewer for spectral data. | ||
+ | * [[Jmol_JavaScript_Object/WebGL|WebGL modality]] to render the 3D model using a combination of HTML5 and WebGL graphics (in compatible systems) | ||
+ | * [[Jmol_JavaScript_Object/ChemDoodle|ChemDoodle option]] to render the 3D model using ChemDoodle | ||
− | === | + | === Setting parameters === |
+ | The essential and minimal call to create a JSmol object is simply: | ||
− | + | <code>Jmol.getApplet("myJmol")</code> | |
− | + | This will create a <tt>myJmol</tt> global variable in JavaScript that holds the JSmol object and is also the unique ID for that object in all functions and methods described below. ''Note that this simplest syntax will only work when the html file is located in the root JSmol folder.'' | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | However, in most situations you will want to customize some aspects, like the size of the object and the file paths. All of the initialization parameters can be specified. | |
− | + | The regular call to create a JSmol object with specified characteristics is | |
+ | to define an <code>Info</code> variable, which is an associative array (a set of key+value pairs) that indicates all the desired characteristics of the JSmol object. | ||
+ | The JSmol.min.js library will provide a default <code>Info</code> variable, so you only need to specify those keys which values you want to customize. | ||
− | + | Once <code>Info</code> has been defined, you create and insert the JSmol object in the page using this: | |
− | + | <code>'''Jmol'''.getApplet("myJmol", Info)</code> | |
− | + | :Note that <tt>myJmol</tt> and <tt>Info</tt> are user-defined variables and may hence have any name you wish. <tt>myJmol</tt> becomes in fact the identifier of the particular JSmol object that is being created. You may wish to have two JSmols in your page and call them e.g. <tt>jmolA</tt> and <tt>jmolB</tt>, and use for them the same set of parameters <tt>Info</tt>, or use two different sets named e.g. <tt>InfoA</tt> and <tt>InfoB</tt>. In contrast, <tt>'''Jmol'''</tt> (right at the beginning) must be written as such, since it is the internal name and identification of the unique JSmol object constructor. | |
− | + | For a start, you may just copy and then adapt this simple example: | |
− | + | <pre> | |
− | < | + | var Info = { |
+ | color: "#FFFFFF", | ||
+ | height: 300, | ||
+ | width: 300, | ||
+ | script: "load $caffeine", | ||
+ | use: "HTML5", | ||
+ | j2sPath: "j2s", | ||
+ | serverURL: "php/jsmol.php", | ||
+ | }; | ||
− | + | Jmol.getApplet("myJmol", Info); | |
+ | </pre> | ||
− | + | What this will do is: | |
+ | * Create a JavaScript variable (of the object type) named <code>myJmol</code>. | ||
+ | * Insert a JSmol instance at that point in the web page, 300×300 pixels in size, with white background. | ||
+ | * Retrieve data for caffeine from the Cactus server and display the molecular structure in the JSmol panel. | ||
− | + | If you want more control, keep reading. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | If | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | <span style="font-size:1.2em;"> | ||
+ | A '''[[Jmol_JavaScript_Object/Info|detailed explanation]] of the parameters''' included in the <code>Info</code> variable | ||
+ | </span> | ||
+ | is available in a separate page, while the major ones and their default values are given below: | ||
+ | <pre> | ||
+ | var Info = { | ||
+ | color: "#FFFFFF", // white background (note this changes legacy default which was black) | ||
+ | height: 300, // pixels (but it may be in percent, like "100%") | ||
+ | width: 300, | ||
+ | use: "HTML5", // "HTML5" or "Java" (case-insensitive) | ||
+ | j2sPath: "j2s", // only used in the HTML5 modality | ||
+ | jarPath: "java", // only used in the Java modality | ||
+ | jarFile: "JmolApplet0.jar", // only used in the Java modality | ||
+ | isSigned: false, // only used in the Java modality | ||
+ | serverURL: "php/jsmol.php", // this is not applied by default; you should set this value explicitly | ||
+ | src: null, // file to load | ||
+ | script: null, // script to run | ||
+ | defaultModel: "", // name or id of a model to be retrieved from a database | ||
+ | addSelectionOptions: false, // to interface with databases | ||
+ | disableInitialConsole: false, // shows a bunch of messages while the object is being built | ||
+ | debug: false | ||
+ | }; | ||
+ | </pre> | ||
− | == | + | ==== Setting parameters from the URL ==== |
+ | ''Jmol 14.0 or later'' | ||
− | + | Some values in the <tt>Info</tt> variable defined in the page may be overriden by the user, by adding a parameter in the page URL. This may be useful for testing, both own and some else's pages. | |
− | |||
− | + | The settable options affect | |
+ | * what modality of J(S)mol to use, either Java or HTML5 | ||
+ | * what kind of applet to use, either 'sandboxed' or 'all-permissions' (new terms, more or less equivalent to the formerly called unsigned and signed applets) | ||
+ | * whether to get the JmolApplet files from some other location (url) | ||
+ | * whether to get the JavaScript files that build the Jmol Object from some other location (url) | ||
− | + | The format for parameters in the url is the standard syntax in so called 'search' part of the url, i.e. <code>?parameter1=value1¶meter2=value2&...etc.</code> | |
− | + | Examples (you get the meaning, don't you?): | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<pre> | <pre> | ||
− | + | any.htm?_USE=html5 | |
+ | any.htm?_USE=java | ||
+ | any.htm?_USE=signed | ||
+ | any.htm?_JAR=http://some.url.com/some/jsmol/java | ||
+ | any.htm?_J2S=http://some.url.com/some/jsmol/j2s | ||
+ | any.htm?_USE=java&_JAR=http://some.url.com/some/jsmol/java | ||
+ | any.htm?_USE=html5&_J2S=http://some.url.com/some/jsmol/j2s | ||
</pre> | </pre> | ||
− | + | Note that the names, like <code>_USE</code>, must be uppercase. The values are case-insensitive. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | == Functions for Jmol JavaScript Object == | ||
− | + | Please see [[Jmol_JavaScript_Object/Functions]] for details about: | |
− | + | * Functions for creation or display of Jmol Objects | |
− | * | + | * Functions for creating controls |
+ | * Functions that insert HTML | ||
+ | * Functions that set CSS rules | ||
+ | * Functions that interact with a running Jmol Object | ||
+ | * Deprecated, unnecessary or not recommended | ||
+ | * Methods specific to optional components |
Latest revision as of 16:00, 18 April 2024
JSmol, the Jmol JavaScript Object
The heart of JSmol is the Jmol JavaScript object, (window.Jmol
), which includes a set of JavaScript functions and utilities. The initial Jmol object was developed by Bob Hanson and Paul Pillot in early 2012. It was folded into JSmol later that year. The library JSmol.min.js provides this object and supersedes the Jmol.js JavaScript library formerly used exclusively with the Jmol Java applet, allowing a cleaner, more efficient way to interact with Jmol on a web page, and abstracting the visualization of a molecular model so that the Java applet can be seamlessly and selectively replaced by a non-Java HTML5 canvas element (which might not even be Jmol).
Using JSmol, a web page developer can target most web browsers that no longer support Java applets, using a Jmol applet surrogate, while still implementing the Jmol Java applet on compatible platforms.
JSmol also integrates facilities for direct, behind-the-scenes access to public databases such as the RCSB PDB database, the National Cancer Institute CACTVS server, and PubChem.
In addition, JSmol allows easy interaction with
- the JSpecView HTML5 object, allowing one to tie 3D models to IR, NMR, UV/VIS, GC, and GC/MS spectra (see more details)
- the JSME HTML5 object, so 3D models can be generated by a page visitor using simple chemical 2D drawing (see more details)
JSmol is fully compatible with jQuery 1.9 or 1.10. (jQuery 2.0 does not and for reasons of principle reportedly will never support MSIE running locally using file://, so if you do not need to run your page in MSIE in a local environment, that should be fine. There is a fix for this, however, if you must do that. In that case you must hack jQuery a bit.) The library has been W3C and XHTML validated.
Main features of JSmol
JSmol
JSmol is the name for the HTML5 canvas version of the former Jmol applet. JSmol opens up the use of Jmol in PC, Mac, and Linux systems, tablets and phones (both iOS and Android) without the need for Java. No hardware-based graphics acceleration is used, allowing JSmol to run in any web browser that supports HTML5 standards. JSmol runs entirely in the client, needing no server technologies for most of its operation. (Reading binary files in some browsers and saving images and Jmol states in all browsers do require a server-side PHP script, commonly provided as php/jsmol.php –read details below.)
JSmol was developed by Bob Hanson, Zhou Renjian, and Takanori Nakane.
JSmol allows rendering, scripting and interaction with the models just as Jmol does, since the source code is shared by both. Note that JSmol is not a different program than Jmol: it is Jmol, just compiled into JavaScript instead of Java (thanks to the Java2Script software).
Current limitations of JSmol
- Using local files (i.e. not in a web server) is not supported on some browsers due to their security policies:
- Opera allows no access to local files.
- MSIE allows access to local files only if they are not binary.
- Chrome allows access only when Chrome has been started using a special command-line flag (
chrome.exe --allow-file-access-from-files
). - Firefox allows access only after setting a special
about:config
flag (security.fileuri.strict_origin_policy = false
).
- Opening binary files (e.g Spartan files, gzipped files, pngj files): they can be read but must be identified as such in their file name (see below).
- Writing of jpg, png and pngj (png+zip) data is fully supported, but delivering it to a user may require a server-side piece. (See also Exporting an image for alternatives).
- The Jmol
prompt
command does not allow more than a simple JavaScript-like response in the HTML5 version. - The WebGL modality of JSmol has not been fully developed and feature support is limited.
Reading binary files
Jmol (Java) can open a file and then determine whether the file is binary or not. JSmol (JavaScript) is different, because the AJAX transport mechanism is different for binary and nonbinary files. Thus, JSmol must determine whether a file is binary or not prior to its loading.
File names
JSmol will determine whether a file is binary or not —before loading it— by inspection of the file name. JSmol will switch to binary mode for files with any of the following in their file name: .bin .gz .zip .jpg .png .jmol .smol .spartan .mrc .pse
These "extensions" can appear anywhere in a file name to trigger the binary access mode. So, for example, if you rename any file to include .bin
anywhere in its name, that will instruct JSmol to read it as a binary file.
- Advanced: If you need a different extension to be read as binary, and cannot change the file names to include one of the default extensions, it is possible to extend the set using this code in your JavaScript after the Jmol object has being initialized:
Jmol._binaryTypes.push('.myExtension');
Browser behaviour
For binary file reading to be compatible with Chrome and MSIE, you will need to have a base64 server-side piece that will convert the binary data to BASE64
format. This is because only Firefox allows clean (reliable) synchronous binary file transfer. (And, so far, we have not figured out how to move all the file loading in Jmol to a fully asynchronous mode.)
If your page visitors need to read binary files, place a copy of the jsmol.php file that is included in the JSmol distribution (see below) on your server and point to it using the serverURL item of the Info array. As an example, the demo pages use this approach.
For that to work, your server must support PHP. Otherwise, you might "borrow" by pointing to the jsmol.php file in another server that can run PHP (please, ask the owner of that server).
In Chrome and also other browsers, you may overcome problems with loading local binary files by running pages through a local web server.
Converting legacy web pages to JSmol
It is not hard to convert pages that used the Jmol Applet and the Jmol.js
library to use the HTML5 versions of JSmol. See more details.
Installation
See Jmol JavaScript Object/Installation
Initialization
Browser issues
JSmol makes strong use of the HTML5 features. Therefore, it is only compatible with modern web browsers. Specifically, Internet Explorer must be version 9 or higher.
In addition, it is important to use a doctype in the header of the html page. The recommended doctype is the simple one, associated to HTML5 standard, as the very first line of your html document.
Also, for full compatibility, particularly for the localization (language translations) of the JSmol pop-up menu, you should declare the charset as UTF-8 and save the html document (and all accessory files) using UTF-8 encoding (usually without BOM, but this needs further confirmation; recent versions of Firefox seem to have problems with non-BOM UTF-8 files, and UTF-8 with BOM looks like a better solution).
Therefore your html documents should start as:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8">
Loading the library
The web page should have the following in the head
section (pointing to appropriate paths if not the same folder as the web page as shown here):
<script type="text/javascript" src="JSmol.min.js"></script>
Please note that there may be restrictions on the folder locations where you can put files for the libraries (js), the page (html), the models and scripts. This is imposed by browser security policies (related to both Java security and AJAX calls in JavaScript) and usually applies only to situations where you are using the pages from local disk (as opposed to a web server).
- Successful file access by JSmol depends upon whether the page is loaded using "file:" or "http:"
- All this needs confirmation. See also Java security policy which applies to the Java applet.
Lightweight JSmol
There is an alternative version of Jmol object that, using the HTML5 canvas like JSmol, loads very fast while offering minimal functionality (just a simplified ball and stick rendering, no scripting). It is specially aimed at smartphones and such systems with limited resources.
For more details on how to use this, see Lightweight JSmol.
Optional components
- Integration with JSME, an editor to draw 2D chemical structures (Peter Ertl's JavaScript Molecular Editor)
- Integration with JSpecView, a viewer for spectral data.
- WebGL modality to render the 3D model using a combination of HTML5 and WebGL graphics (in compatible systems)
- ChemDoodle option to render the 3D model using ChemDoodle
Setting parameters
The essential and minimal call to create a JSmol object is simply:
Jmol.getApplet("myJmol")
This will create a myJmol global variable in JavaScript that holds the JSmol object and is also the unique ID for that object in all functions and methods described below. Note that this simplest syntax will only work when the html file is located in the root JSmol folder.
However, in most situations you will want to customize some aspects, like the size of the object and the file paths. All of the initialization parameters can be specified.
The regular call to create a JSmol object with specified characteristics is
to define an Info
variable, which is an associative array (a set of key+value pairs) that indicates all the desired characteristics of the JSmol object.
The JSmol.min.js library will provide a default Info
variable, so you only need to specify those keys which values you want to customize.
Once Info
has been defined, you create and insert the JSmol object in the page using this:
Jmol.getApplet("myJmol", Info)
- Note that myJmol and Info are user-defined variables and may hence have any name you wish. myJmol becomes in fact the identifier of the particular JSmol object that is being created. You may wish to have two JSmols in your page and call them e.g. jmolA and jmolB, and use for them the same set of parameters Info, or use two different sets named e.g. InfoA and InfoB. In contrast, Jmol (right at the beginning) must be written as such, since it is the internal name and identification of the unique JSmol object constructor.
For a start, you may just copy and then adapt this simple example:
var Info = { color: "#FFFFFF", height: 300, width: 300, script: "load $caffeine", use: "HTML5", j2sPath: "j2s", serverURL: "php/jsmol.php", }; Jmol.getApplet("myJmol", Info);
What this will do is:
- Create a JavaScript variable (of the object type) named
myJmol
. - Insert a JSmol instance at that point in the web page, 300×300 pixels in size, with white background.
- Retrieve data for caffeine from the Cactus server and display the molecular structure in the JSmol panel.
If you want more control, keep reading.
A detailed explanation of the parameters included in the Info
variable
is available in a separate page, while the major ones and their default values are given below:
var Info = { color: "#FFFFFF", // white background (note this changes legacy default which was black) height: 300, // pixels (but it may be in percent, like "100%") width: 300, use: "HTML5", // "HTML5" or "Java" (case-insensitive) j2sPath: "j2s", // only used in the HTML5 modality jarPath: "java", // only used in the Java modality jarFile: "JmolApplet0.jar", // only used in the Java modality isSigned: false, // only used in the Java modality serverURL: "php/jsmol.php", // this is not applied by default; you should set this value explicitly src: null, // file to load script: null, // script to run defaultModel: "", // name or id of a model to be retrieved from a database addSelectionOptions: false, // to interface with databases disableInitialConsole: false, // shows a bunch of messages while the object is being built debug: false };
Setting parameters from the URL
Jmol 14.0 or later
Some values in the Info variable defined in the page may be overriden by the user, by adding a parameter in the page URL. This may be useful for testing, both own and some else's pages.
The settable options affect
- what modality of J(S)mol to use, either Java or HTML5
- what kind of applet to use, either 'sandboxed' or 'all-permissions' (new terms, more or less equivalent to the formerly called unsigned and signed applets)
- whether to get the JmolApplet files from some other location (url)
- whether to get the JavaScript files that build the Jmol Object from some other location (url)
The format for parameters in the url is the standard syntax in so called 'search' part of the url, i.e. ?parameter1=value1¶meter2=value2&...etc.
Examples (you get the meaning, don't you?):
any.htm?_USE=html5 any.htm?_USE=java any.htm?_USE=signed any.htm?_JAR=http://some.url.com/some/jsmol/java any.htm?_J2S=http://some.url.com/some/jsmol/j2s any.htm?_USE=java&_JAR=http://some.url.com/some/jsmol/java any.htm?_USE=html5&_J2S=http://some.url.com/some/jsmol/j2s
Note that the names, like _USE
, must be uppercase. The values are case-insensitive.
Functions for Jmol JavaScript Object
Please see Jmol_JavaScript_Object/Functions for details about:
- Functions for creation or display of Jmol Objects
- Functions for creating controls
- Functions that insert HTML
- Functions that set CSS rules
- Functions that interact with a running Jmol Object
- Deprecated, unnecessary or not recommended
- Methods specific to optional components
Contributors
AngelHerraez, Hansonr, Hansonrstolaf, Cudo29, Ceroni, Ppillot