Chapter 2. Interacting with JavaScript and Web Pages

Since ClojureScript compiles to JavaScript, you need to have a way to interact with native JavaScript and with web pages. In this chapter, you will discover five different ways to do this:

  1. Direct use of JavaScript
  2. The Google Closure library
  3. The Dommy library
  4. The Domina library
  5. The Enfocus library
Note

All of these methods are fairly “old school.” As of this writing, all the Cool Kids™ are using libraries such as Facebook’s React to handle the user interface. I still think it is useful to have knowledge of the older methods, as they might sometimes be the right tool to solve a problem. Chapter 5 describes how to work with React.

You’ll be doing the same task with each of these: calculating the number of hours of daylight based on a latitude and Julian date, as in “Étude 1-5: More Practice with def and let”. Here is the relevant HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Daylight Minutes</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <h1>Daylight Minutes</h1>
        <p> 
        Latitude: <input type="text" size="8" id="latitude" />&#176;<br />
        Day of year: <input type="text" size="4" id="julian" /><br />
        <input type="button" value="Calculate" id="calculate"/>
        </p>

        <p> 
        Minutes of daylight: <span id="result"></span>
        </p>

        <script src="out/project_name.js" type="text/javascript"></script>
    </body>
</html>

I suggest you create a new project for each of these études and copy the preceding ...

Get Etudes for ClojureScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.