Did you know ... | Search Documentation: |
library(http/js_write): Utilities for including JavaScript |
This library is a supplement to library(http/html_write)
for producing JavaScript fragments. Its main role is to be able to call
JavaScript functions with valid arguments constructed from Prolog data.
For example, suppose you want to call a JavaScript functions to process
a list of names represented as Prolog atoms. This can be done using the
call below, while without this library you would have to be careful to
properly escape special characters.
numbers_script(Names) --> html(script(type('text/javascript'), [ \js_call('ProcessNumbers'(Names) ]),
The accepted arguments are described with js_expression//1.
//
script
element with the given
content.+
operator, which results in concatenation at the client side.
..., js_script({|javascript(Id, Config)|| $(document).ready(function() { $("#"+Id).tagit(Config); }); |}), ...
The current implementation tokenizes the JavaScript input and yields syntax errors on unterminated comments, strings, etc. No further parsing is implemented, which makes it possible to produce syntactically incorrect and partial JavaScript. Future versions are likely to include a full parser, generating syntax errors.
The parser produces a term \List
, which is suitable for
js_script//1 and html//1.
Embedded variables are mapped to
\js_expression(Var)
, while the remaining text is mapped to
atoms.
//
... html(script(type('text/javascript'), [ \js_call('x.y.z'(hello, 42)) ]),
//
['var ', Id, ' = new ', \js_call(Term)]
//
//
null
object(Attributes)
object(Attributes)
, providing a more
JavaScript-like syntax. This may be useful if the object appears
literally in the source-code, but is generally less friendlyto produce
as a result from a computation.json(Term)
true
, false
and null
, but can also be use for emitting JavaScript
symbols (i.e. function- or variable names).symbol(Atom)
//