OClosure Library

OClosure logo

OClosure library is a binding of the Google Closure Javascript library in OCaml using Js_of_ocaml.

You can now write dynamic user interfaces for web pages in OCaml.

To find your way in the hundreds of class types, you may take a look at the API reference but you might also appreciate the Google Closure documentation.


To compile the library, you need:

  • Js_of_ocaml : generate Javascript code from OCaml bytecode

Hello world

1. First, write an OCaml program using OClosure objects:

open Js

let my_alert s =
  let dialog = jsnew Goog.Ui.dialog(Js.null, Js.null, Js.null) in
  dialog##setContent(Js.string s);
  dialog##setVisible(Js._true)

let onload evt =
  let click = Goog.Ui.ControlContent.string (Js.string "Click") in
  let button = jsnew Goog.Ui.button(click, Js.null, Js.null) in
  ignore
    (Goog.Events.listen
       (Goog.Tools.Union.i1 button)
       (Js.string "action")
       (Js.wrap_callback (fun _ -> my_alert "Hello world!"))
       Js.null);
  button##render(Js.some Dom_html.document##body);
  Js._true

let