Multiple Pages

This example shows how you could use GHCJS with an existing web site. To keep it simple this example uses static HTML pages.

The source for this example is here.

Think of your GHCJS application as a custom browser for your web site.

In an ideal world your users would run a web browser with all the features you wanted and there would be no need for JavaScript. Instead we use JavaScript to add the features that are missing.

With GHCJS you can do this by writing a simple browser app like this one using WebKitGtk. Then GHCJS will compile it to JavaScript and you can include it in all your HTML.

One slight complication is that you don't want to wind up running your code twice when you open the HTML in the webkit browser. The ghcjs-dom package has a runWebGUI function. As well as creating a WebKitGtk browser window in native mode, it uses the UserAgent to identify when the JavaScript version is running inside the Native version.

Every time you load a page the Main.main function runs and looks for content on the page to augment. It can use lazy loading to limit the amount of JavaScript loaded for pages with little dynamic content.

To build & run in native code

With the regular GHC or integrated GHCJS installed.

cd multiple-pages
cabal install
multiple-pages http://localhost/.cabal/share/mulitple-pages-0.0.1/index.html

To build & run in any browser

With integrated GHCJS installed.

cd multiple-pages
cabal install
ghcjs-min ~/.cabal/bin/multiple-pages.jsexe data/main.js
open http://localhost/.cabal/share/mulitple-pages-0.0.1/index.html