   event.initEvent 'page:change', true, true
   document.dispatchEvent event
 
+createDocument = do ->
+  createDocumentUsingParser = (html) ->
+    (new DOMParser).parseFromString html, "text/html"
+
+  createDocumentUsingWrite = (html) ->
+    doc = document.implementation.createHTMLDocument ""
+    doc.open "replace"
+    doc.write html
+    doc.close
+    doc
+
+  if window.DOMParser
+    testDoc = createDocumentUsingParser "<html><body><p>test"
+
+  if testDoc?.body?.childNodes.length is 1
+    createDocumentUsingParser
+  else
+    createDocumentUsingWrite
+
 replaceHTML = (html) ->
-  doc = document.implementation.createHTMLDocument ""
-  doc.open "replace"
-  doc.write html
-  doc.close()
-
+  doc = createDocument html
   originalBody = document.body
   document.documentElement.appendChild doc.body, originalBody
   document.documentElement.removeChild originalBody

    handleClick event

# Call Turbolinks.visit(url) from client code
@Turbolinks = { visit: visit }

extractLink = (event) ->
  link = event.target
  link = link.parentNode until link is document or link.nodeName is 'A'
  link

crossOriginLink = (link) ->
