   # Earl, don't fall asleep on the job!
   react_to_location()
 
-if window.addEventListener
-  window.addEventListener 'load', onload, false 
-else if window.attachEvent
-  window.attachEvent 'onload', onload
+window.addEventListener?('load', onload, false) or window.attachEvent?('onload', onload)
 
 
 

  search = location.search?.replace(/\%2[fF]/g, '/')
  loc = location.pathname?.replace(/\%20/g, ' ')

  if Earl.root
    loc = (loc + '/').split(Earl.root)[1]

  "#{loc}#{search}#{location.hash}"

url_from_statebus = ->
  loc = fetch 'location'

  relative_url = loc.url or '/'

  if loc.query_params && Object.keys(loc.query_params).length > 0
    query_params = ("#{k}=#{v}" for own k,v of loc.query_params)
    relative_url += "?#{query_params.join('&')}" 
  if loc.hash?.length > 0
    relative_url += "##{loc.hash}"

  relative_url


# For handling device-specific annoyances

  dom.A = ->
    props = @props
    if @props.href
      href = @props.href

      # Earl will call a click handler that the programmer passes in
      onClick = @props.onClick or (-> null)      

      # Earl rewrites the address when necessary 
      internal_link = !href.match('//') || !!href.match(location.origin)
      is_mailto = !!href.toLowerCase().match('mailto')
      if internal_link && !is_mailto
        if href.substring(0, Earl.root.length) != Earl.root
          rel = href[0] != '/'
          if rel 
            href = loc.path + '/' + href 
          else  
            href = Earl.root + href 
          href = @props.href = href.replace('//', '/')



      handle_click = (event) =>
        opened_in_new_tab = event.altKey  || \
                            event.ctrlKey || \
                            event.metaKey || \
                            event.shiftKey


        # In his wisdom, Earl sometimes just lets the default behavior occur
        if !internal_link || opened_in_new_tab || is_mailto || @props.target == '_blank'
          onClick event

        # ... but other times Earl is history aware
        else 
          event.preventDefault()
          event.stopPropagation()

          Earl.load_page href
          onClick event

