aktowns/purescript-simple-dom


License: MIT

Language: PureScript


purescript-simple-dom

Build Status

About

An easy to use library for dealing with the DOM and general javascript APIs. Minimal dependencies, and aiming to be as close to the javascript API as possible.

Module Documentation

Some Examples

Set the contents of an iframe to arbitary html content

setContents contents = do
  -- doc = window.document
  doc <- document globalWindow
  -- iframe = doc.querySelector("#siteFrame")
  iframe <- querySelector "#siteFrame" doc
  -- iframeDoc = iframe.contentWindow.document
  iframeDoc <- (contentWindow iframe) >>= document
  -- iframeDoc.querySelector("html").innerHTML = contents
  querySelector "html" iframeDoc >>= setInnerHTML contents

-- or a little shorter
setContents' contents = do
  document globalWindow >>= querySelector "#siteFrame" >>=
    contentWindow >>= document >>= querySelector "html" >>=
      setInnerHTML contents

Change all a href's on a page and add the original link as a data attribute

modifyLinkTarget link = do
  attr <- getAttribute "href" link       -- attr = link.getAttribute("href")
  setAttribute "href" "#" link           -- link.setAttribute("href", "#")
  setAttribute "data-target" attr link   -- link.setAttribute("data-target", attr)
  return link

modifyLinks page = do
  links <- querySelectorAll "a" page      -- links = [HTMLElement]
  sequence $ A.map modifyLinkTarget links -- links.map(modifyLinkTarget)

Place some content from an API call into a div

-- Convert the evt content into text for the callback
handleRequest callback evt = do
  target <- eventTarget event
  text <- responseText target
  callback text

-- Construct and perform AJAX request for the specified url
makeGetRequest url callback = do
  req <- makeXMLHttpRequest
  addEventListener "load" (handleRequest callback) req
  open GET url req
  setRequestHeader "Content-Type" "application/json" req
  send NoData req

-- retrieve the content and place it inside the div
requestContent = do
  let url = "http://myendpoint.com/"
  doc <- document globalWindow

  makeGetRequest url $ \resp -> do
    querySelector "#myDiv" doc >>= setInnerHtml resp

Project Statistics

Sourcerank 7
Repository Size 205 KB
Stars 40
Forks 28
Watchers 8
Open issues 9
Dependencies 18
Contributors 16
Tags 8
Created
Last updated
Last pushed

Top Contributors See all

Ashley Towns codygman Joseph Wynn Jonathan Childress David Peter Dylan Simon Rick Elrod Chris Waterson Brian McKenna Michael Xavier Harry Garrood caryoscelus Ingmar Gagen matthieubulte Gary Burgess Simeon Visser

Packages Referencing this Repo

purescript-simple-dom
An easy to use library for dealing with the DOM and general javascript APIs.
Latest release 0.1.3 - Published - 40 stars

Recent Tags See all

0.1.3 November 02, 2015
0.1.2 November 02, 2015
0.1.1 November 02, 2015
0.1.0 August 05, 2015
0.0.4 August 03, 2015
0.0.3 August 03, 2015
0.0.2 April 13, 2015
0.0.1 January 24, 2015

Something wrong with this page? Make a suggestion

Last synced: 2016-05-27 17:44:08 UTC

Login to resync this repository