- Documentation
- Reference manual
- Packages
- SWI-Prolog HTTP support
- The HTTP server libraries
- Creating an HTTP reply
- library(http/http_dispatch): Dispatch requests in the HTTP server
- library(http/http_dirindex): HTTP directory listings
- library(http/http_files): Serve plain files from a hierarchy
- library(http/http_session): HTTP Session management
- library(http/http_cors): Enable CORS: Cross-Origin Resource Sharing
- library(http/http_authenticate): Authenticate HTTP connections using 401 headers
- library(http/http_digest): HTTP Digest authentication
- library(http/http_dyn_workers): Dynamically schedule HTTP workers.
- Custom Error Pages
- library(http/http_openid): OpenID consumer and server library
- Get parameters from HTML forms
- Request format
- Running the server
- The wrapper library
- library(http/http_host): Obtain public server location
- library(http/http_log): HTTP Logging module
- Debugging HTTP servers
- library(http/http_header): Handling HTTP headers
- The library(http/html_write) library
- library(http/js_write): Utilities for including JavaScript
- library(http/http_path): Abstract specification of HTTP server locations
- library(http/html_head): Automatic inclusion of CSS and scripts links
- library(http/http_pwp): Serve PWP pages through the HTTP server
- The HTTP server libraries
- SWI-Prolog HTTP support
3.21 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.
- [det]js_script(+Content)
//
- Generate a JavaScript
script
element with the given content. - [det]javascript(+Content, +Vars, +VarDict, -DOM)
- Quasi quotation parser for JavaScript that allows for embedding Prolog
variables to substitude identifiers in the JavaScript snippet.
Parameterizing a JavaScript string is achieved using the JavaScript
+
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.- To be done
- Implement a full JavaScript parser. Users should not rely on the ability to generate partial JavaScript snippets.
- [det]js_call(+Term)
//
- Emit a call to a Javascript function. The Prolog functor is the name of
the function. The arguments are converted from Prolog to JavaScript
using js_arg_list//1. Please
not that Prolog functors can be quoted atom and thus the following is
legal:
... html(script(type('text/javascript'), [ \js_call('x.y.z'(hello, 42)) ]),
- [det]js_new(+Id,
+Term)
//
- Emit a call to a Javascript object declaration. This is the same as:
['var ', Id, ' = new ', \js_call(Term)]
- [det]js_arg_list(+Expressions:list)
//
- Write javascript (function) arguments. This writes "(", Arg, ..., ")". See js_expression//1 for valid argument values.
- [det]js_expression(+Expression)
//
- Emit a single JSON argument. Expression is one of:
- Variable
- Emitted as Javascript
null
- List
- Produces a Javascript list, where each element is processed by this library.
object(Attributes)
- Where Attributes is a Key-Value list where each pair can be written as
Key-Value, Key=Value or Key(Value), accomodating all common constructs
for this used in Prolog.
$ { K:V, ... } Same as
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. - Dict
- Emit a dict as a JSON object using json_write_dict/3.
json(Term)
- Emits a term using json_write/3.
- @(Atom)
- Emits these constants without quotes. Normally used for the symbols
true
,false
andnull
, but can also be use for emitting JavaScript symbols (i.e. function- or variable names). - Number
- Emited literally
symbol(Atom)
- Synonym for @(Atom). Deprecated.
- Atom or String
- Emitted as quoted JavaScript string.
- [semidet]js_arg(+Expression)
//
- Same as js_expression//1,
but fails if Expression is invalid, where js_expression//1
raises an error.
- deprecated
- New code should use js_expression//1.