Module Js_of_ocaml.Js
Javascript binding
This module provides types and functions to interoperate with Javascript values, and gives access to Javascript standard objects.
Dealing with null and undefined values.
val null : 'a optThe
nullvalue.
val some : 'a -> 'a optConsider a value into a possibly null value.
val undefined : 'a optdefThe
undefinedvalue
val def : 'a -> 'a optdefConsider a value into a possibly undefined value.
module type OPT = sig ... endSignatures of a set of standard functions for manipulating optional values.
Types for specifying method and properties of Javascript objects
type +'a tType of Javascript objects. The type parameter is used to specify more precisely an object.
type +'a methType used to specify method types: a Javascript object
<m : t1 -> t2 -> ... -> tn -> t Js.meth> Js.thas a Javascript methodmexpecting n arguments of typest1totnand returns a value of typet.
type +'a gen_propType used to specify the properties of Javascript objects. In practice you should rarely need this type directly, but should rather use the type abbreviations below instead.
type 'a readonly_prop= < get : 'a; > gen_propType of read-only properties: a Javascript object
<p : t Js.readonly_prop> Js.thas a read-only propertypof typet.
type 'a writeonly_prop= < set : 'a -> unit; > gen_propType of write-only properties: a Javascript object
<p : t Js.writeonly_prop> Js.thas a write-only propertypof typet.
type 'a prop= < get : 'a; set : 'a -> unit; > gen_propType of read/write properties: a Javascript object
<p : t Js.writeonly_prop> Js.thas a read/write propertypof typet.
Object constructors
Callbacks to OCaml
type (-'a, +'b) meth_callbackType of callback functions. A function of type
(u, t1 -> ... -> tn -> t) meth_callbackcan be called from Javascript withthisbound to a value of typeuand up to n arguments of typest1totn. The system takes care of currification, so less than n arguments can be provided. As a special case, a callback of type(t, unit -> t) meth_callbackcan be called from Javascript with no argument. It will behave as if it was called with a single argument of typeunit.
type 'a callback= (unit, 'a) meth_callbackType of callback functions intended to be called without a meaningful
thisimplicit parameter.
val wrap_callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callbackWrap an OCaml function so that it can be invoked from Javascript.
val wrap_meth_callback : ('b -> 'a) -> ('b, 'a) meth_callbackWrap an OCaml function so that it can be invoked from Javascript. The first parameter of the function will be bound to the value of the
thisimplicit parameter.
Javascript standard objects
val _true : bool tJavascript
trueboolean.
val _false : bool tJavascript
falseboolean.
type match_result_handleA handle to a match result. Use function
Js.match_resultto get the correspondingMatchResultobject. (This type is used to resolved the mutual dependency between string and array type definitions.)
type string_arrayOpaque type for string arrays. You can get the actual
Arrayobject using functionJs.str_array. (This type is used to resolved the mutual dependency between string and array type definitions.)
class type js_string = object ... endSpecification of Javascript string objects.
and regExp = object ... endSpecification of Javascript regular expression objects.
class type string_constr = object ... endSpecification of the string constructor, considered as an object.
val string_constr : string_constr tThe string constructor, as an object.
val regExp : (js_string t -> regExp t) constrConstructor of
RegExpobjects. The expressionjsnew regExp (s)builds the regular expression specified by strings.
val regExp_withFlags : (js_string t -> js_string t -> regExp t) constrConstructor of
RegExpobjects. The expressionjsnew regExp (s, f)builds the regular expression specified by stringsusing flagsf.
val regExp_copy : (regExp t -> regExp t) constrConstructor of
RegExpobjects. The expressionjsnew regExp (r)builds a copy of regular expressionr.
class type 'a js_array = object ... endSpecification of Javascript regular arrays. Use
Js.array_getandJs.array_setto access and set array elements.
val object_keys : 'a t -> js_string t js_array tReturns jsarray containing keys of the object as Object.keys does.
val array_empty : 'a js_array t constrConstructor of
Arrayobjects. The expressionjsnew array_empty ()returns an empty array.
val array_length : (int -> 'a js_array t) constrConstructor of
Arrayobjects. The expressionjsnew array_length (l)returns an array of lengthl.
val array_get : 'a js_array t -> int -> 'a optdefArray access:
array_get a ireturns the element at indexiof arraya. Returnsundefinedif there is no element at this index.
val array_set : 'a js_array t -> int -> 'a -> unitArray update:
array_set a i vputsvat indexiin arraya.
val array_map : ('a -> 'b) -> 'a js_array t -> 'b js_array tArray map:
array_map f aisa##map(wrap_callback (fun elt idx arr -> f elt)).
val array_mapi : (int -> 'a -> 'b) -> 'a js_array t -> 'b js_array tArray mapi:
array_mapi f aisa##map(wrap_callback (fun elt idx arr -> f idx elt)).
class type match_result = object ... endSpecification of match result objects
val str_array : string_array t -> js_string t js_array tConvert an opaque
string_array tobject into an array of string. (Used to resolved the mutual dependency between string and array type definitions.)
val match_result : match_result_handle t -> match_result tConvert a match result handle into a
MatchResultobject. (Used to resolved the mutual dependency between string and array type definitions.)
class type number = object ... endSpecification of Javascript number objects.
class type date = object ... endSpecification of Javascript date objects.
val date_now : date t constrConstructor of
Dateobjects:jsnew date_now ()returns aDateobject initialized with the current date.
val date_fromTimeValue : (float -> date t) constrConstructor of
Dateobjects:jsnew date_fromTimeValue (t)returns aDateobject initialized with the time valuet.
val date_month : (int -> int -> date t) constrConstructor of
Dateobjects:jsnew date_fromTimeValue (y, m)returns aDateobject corresponding to yearyand monthm.
val date_day : (int -> int -> int -> date t) constrConstructor of
Dateobjects:jsnew date_fromTimeValue (y, m, d)returns aDateobject corresponding to yeary, monthmand dayd.
val date_hour : (int -> int -> int -> int -> date t) constrConstructor of
Dateobjects:jsnew date_fromTimeValue (y, m, d, h)returns aDateobject corresponding to yearyto hourh.
val date_min : (int -> int -> int -> int -> int -> date t) constrConstructor of
Dateobjects:jsnew date_fromTimeValue (y, m, d, h, m')returns aDateobject corresponding to yearyto minutem'.
val date_sec : (int -> int -> int -> int -> int -> int -> date t) constrConstructor of
Dateobjects:jsnew date_fromTimeValue (y, m, d, h, m', s)returns aDateobject corresponding to yearyto seconds.
val date_ms : (int -> int -> int -> int -> int -> int -> int -> date t) constrConstructor of
Dateobjects:jsnew date_fromTimeValue (y, m, d, h, m', s, ms)returns aDateobject corresponding to yearyto millisecondms.
class type date_constr = object ... endSpecification of the date constructor, considered as an object.
val date : date_constr tThe date constructor, as an object.
class type math = object ... endSpecification of Javascript math object.
class type error = object ... endSpecification of Javascript error object.
val error_constr : (js_string t -> error t) constrConstructor of
Errorobjects:jsnew error_constr (msg)returns anErrorobject with the messagemsg.
exceptionError of error tThe
Errorexception wrap javascript exceptions when caught by OCaml code. In case the javascript exception is not an instance of javascriptError, it will be serialized and wrapped into aFailureexception.
class type json = object ... endSpecification of Javascript JSON object.
Standard Javascript functions
val decodeURI : js_string t -> js_string tDecode a URI: replace by the corresponding byte all escape sequences but the ones corresponding to a URI reserved character and convert the string from UTF-8 to UTF-16.
val decodeURIComponent : js_string t -> js_string tDecode a URIComponent: replace all escape sequences by the corresponding byte and convert the string from UTF-8 to UTF-16.
val encodeURI : js_string t -> js_string tEncode a URI: convert the string to UTF-8 and replace all unsafe bytes by the corresponding escape sequence.
val encodeURIComponent : js_string t -> js_string tSame as
encodeURI, but also encode URI reserved characters.
val escape : js_string t -> js_string tEscape a string: unsafe UTF-16 code points are replaced by 2-digit and 4-digit escape sequences.
Conversion functions between Javascript and OCaml types
val bool : bool -> bool tConversion of booleans from OCaml to Javascript.
val to_bool : bool t -> boolConversion of booleans from Javascript to OCaml.
val string : string -> js_string tConversion of strings from OCaml to Javascript. (The OCaml string is considered to be encoded in UTF-8 and is converted to UTF-16.)
Convenience coercion functions
val coerce : 'a -> ('a -> 'b Opt.t) -> ('a -> 'b) -> 'bApply a possibly failing coercion function.
coerce v c fattempts to apply coercioncto valuev. If the coercion returnsnull, functionfis called.
val coerce_opt : 'a Opt.t -> ('a -> 'b Opt.t) -> ('a -> 'b) -> 'bApply a possibly failing coercion function.
coerce_opt v c fattempts to apply coercioncto valuev. Ifvisnullor the coercion returnsnull, functionfis called. Typical usage is the following:Js.coerce_opt (Dom_html.document##getElementById id) Dom_html.CoerceTo.div (fun _ -> assert false)
Type checking operators.
Debugging operations.
Export functionality.
Export values to module.exports if it exists or to the global object otherwise.
val export_all : 'a t -> unitexport_all objexport every key ofobjobject.export_all object%js method add x y = x +. y method abs x = abs_float x val zero = 0. end
Unsafe operations.
module Unsafe : sig ... endUnsafe Javascript operations
Deprecated functions and types.
type float_prop= float propType of float properties.