Last active
August 29, 2015 14:06
-
-
Save mcrowe/7e09fd67bbb2e5083059 to your computer and use it in GitHub Desktop.
TurboReload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.8.0 | |
var turboReload; | |
turboReload = function() { | |
var browserCompatibleDocumentParser, documentParser, url; | |
browserCompatibleDocumentParser = function() { | |
var createDocumentUsingDOM, createDocumentUsingParser, createDocumentUsingWrite, e, testDoc, _ref; | |
createDocumentUsingParser = function(html) { | |
return (new DOMParser).parseFromString(html, 'text/html'); | |
}; | |
createDocumentUsingDOM = function(html) { | |
var doc; | |
doc = document.implementation.createHTMLDocument(''); | |
doc.documentElement.innerHTML = html; | |
return doc; | |
}; | |
createDocumentUsingWrite = function(html) { | |
var doc; | |
doc = document.implementation.createHTMLDocument(''); | |
doc.open('replace'); | |
doc.write(html); | |
doc.close(); | |
return doc; | |
}; | |
try { | |
if (window.DOMParser) { | |
testDoc = createDocumentUsingParser('<html><body><p>test'); | |
return createDocumentUsingParser; | |
} | |
} catch (_error) { | |
e = _error; | |
testDoc = createDocumentUsingDOM('<html><body><p>test'); | |
return createDocumentUsingDOM; | |
} finally { | |
if ((testDoc != null ? (_ref = testDoc.body) != null ? _ref.childNodes.length : void 0 : void 0) !== 1) { | |
return createDocumentUsingWrite; | |
} | |
} | |
}; | |
documentParser = browserCompatibleDocumentParser(); | |
url = document.location.href; | |
return $.get(url, function(updatedHtml) { | |
var updatedBody; | |
updatedBody = documentParser(updatedHtml).body; | |
return $('body').html(updatedBody); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Call turboReload() on any page to update the content of the current | |
# page without disturbing the user. | |
# | |
# 1) It is faster than a normal reload because the browser doesn't fetch or | |
# parse any scripts or stylesheets in the head. Instead, we just replace the | |
# body contents with the updated body. | |
# | |
# 2) It doesn't cause the page to scroll or flicker on the user. | |
# | |
turboReload = -> | |
# browserCompatibleDocumentParser, borrowed from TurboLinks. | |
# | |
# Use createDocumentUsingParser if DOMParser is defined and natively | |
# supports 'text/html' parsing (Firefox 12+, IE 10) | |
# | |
# Use createDocumentUsingDOM if createDocumentUsingParser throws an exception | |
# due to unsupported type 'text/html' (Firefox < 12, Opera) | |
# | |
# Use createDocumentUsingWrite if: | |
# - DOMParser isn't defined | |
# - createDocumentUsingParser returns null due to unsupported type 'text/html' (Chrome, Safari) | |
# - createDocumentUsingDOM doesn't create a valid HTML document (safeguarding against potential edge cases) | |
# | |
browserCompatibleDocumentParser = -> | |
createDocumentUsingParser = (html) -> | |
(new DOMParser).parseFromString html, 'text/html' | |
createDocumentUsingDOM = (html) -> | |
doc = document.implementation.createHTMLDocument '' | |
doc.documentElement.innerHTML = html | |
doc | |
createDocumentUsingWrite = (html) -> | |
doc = document.implementation.createHTMLDocument '' | |
doc.open 'replace' | |
doc.write html | |
doc.close() | |
doc | |
try | |
if window.DOMParser | |
testDoc = createDocumentUsingParser '<html><body><p>test' | |
createDocumentUsingParser | |
catch e | |
testDoc = createDocumentUsingDOM '<html><body><p>test' | |
createDocumentUsingDOM | |
finally | |
unless testDoc?.body?.childNodes.length is 1 | |
return createDocumentUsingWrite | |
documentParser = browserCompatibleDocumentParser() | |
url = document.location.href | |
$.get url, (updatedHtml) -> | |
updatedBody = documentParser(updatedHtml).body | |
$('body').html(updatedBody) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment