How to add a Javacript script?

If you have to put some client-side execution on your website, it is hardly recommended to use js_of_ocaml and not including external javascript scripts.

However, if you have to include an external script, for example if you downloaded it on another website, or if you have already made it directly in Javascript, it is possible to include it and use it.

Include the script on the html header

Javascript scripts are included in the header using the js_script function.

     ~uri:(make_uri (Eliom_service.static_dir ())

This function has 2 parameters: the file path and unit.

The file path is generated using the make_uri function. This function creates the relative URL string using the static directory configured in the configuration file and the given list.

Insert this piece of code on the list given in parameter to the head function.

Or you can use: Eliom_tools module

◄ How does a page's source code look?

Call an external function

It is possible to call a function in this file.

However, as the power of Ocsigen is its stability, it is checking at the compilation that functions you are calling exists. If you call a function in the same way as with js_of_ocaml, it will not compile:

Error: Unbound value hello_function

To call an unknown function, you have to use the Unsafe module of js_of_ocaml:

{{ Js.Unsafe.eval_string "hello_function()" }}

Download full code


◄ Back to the Quick Howto's