Skip to content

Instantly share code, notes, and snippets.

@mightybyte
Created September 29, 2015 17:40
Show Gist options
  • Save mightybyte/057876a285585c686566 to your computer and use it in GitHub Desktop.
Save mightybyte/057876a285585c686566 to your computer and use it in GitHub Desktop.
Reflex Location Management
------------------------------------------------------------------------------
-- | Gets the current path of the DOM Window (i.e., the contents of the
-- address bar after the host, beginning with a "/").
-- https://developer.mozilla.org/en-US/docs/Web/API/Location
getWindowLocationPathname :: DOMWindow -> IO String
#ifdef ghcjs_HOST_OS
getWindowLocationPathname w = do
jw <- toJSRef w
liftM fromJSString $ js_windowLocationPathname jw
foreign import javascript unsafe
"$1.location.pathname"
js_windowLocationPathname :: JSRef DOMWindow -> IO JSString
#else
getWindowLocationPathname = error "getWindowLocationPathname: only works in GHCJS"
#endif
------------------------------------------------------------------------------
setWindowLocationHref :: String -> IO ()
#ifdef ghcjs_HOST_OS
setWindowLocationHref = js_setWindowLocationHref . toJSString
foreign import javascript unsafe
"window.location.href=$1"
js_setWindowLocationHref :: JSString -> IO ()
#else
setWindowLocationHref = error "setWindowLocationHref: only works in GHCJS"
#endif
@paldepind
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment