Last active
August 29, 2015 14:14
-
-
Save JamesMGreene/67d1e6c054699f24724c to your computer and use it in GitHub Desktop.
Basic `document.readyState`/`script.readyState` tests
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Basic `document.readyState`/`script.readyState` tests</title> | |
<script id="loadFirst"> | |
function getScriptIds(scriptEls) { | |
var scriptIds = []; | |
if (scriptEls) { | |
for (var i = 0, len = scriptEls.length; i < len; i++) { | |
scriptIds.push(scriptEls[i].id); | |
} | |
} | |
return scriptIds; | |
} | |
function getReadyStates(scriptEls) { | |
var scriptStates = []; | |
if (scriptEls) { | |
for (var i = 0, len = scriptEls.length; i < len; i++) { | |
scriptStates.push(scriptEls[i].readyState); | |
} | |
} | |
return scriptStates; | |
} | |
function toArray(nodeList) { | |
try { | |
return [].slice.call(nodeList); | |
} | |
catch (e) { | |
var arr = []; | |
if (nodeList) { | |
for (var i = 0, len = nodeList.length; i < len; i++) { | |
arr.push(nodeList[i]); | |
} | |
} | |
return arr; | |
} | |
} | |
function showState(name) { | |
var scripts = toArray(document.getElementsByTagName("script")); | |
var scriptCount = scripts.length; | |
var output = []; | |
output.push("<div><strong>[" + name + "] document.readyState:</strong> " + document.readyState + "</div>"); | |
output.push("<div><strong>[" + name + "] scripts.length:</strong> " + scriptCount + "</div>"); | |
output.push("<div><strong>[" + name + "] scripts IDs:</strong> " + getScriptIds(scripts).join(", ") + "</div>"); | |
output.push("<div><strong>[" + name + "] scripts readyStates:</strong> " + getReadyStates(scripts).join(", ") + "</div>"); | |
for (var i = 0, len = output.length; i < len; i++) { | |
if (!!document.readyState || document.readyState === "complete") { | |
document.body.innerHTML += output[i]; | |
} | |
else { | |
document.write(output[i]); | |
} | |
} | |
} | |
document.write("<div><strong>UserAgent:</strong> " + navigator.userAgent + "</div>"); | |
var scripts = document.getElementsByTagName("script"); | |
var current = scripts[0]; | |
var hasRSSupport = "readyState" in current; | |
document.write("<div><strong>script.readyState supported?</strong> " + hasRSSupport + "</div>"); | |
var hasCSSupport = "currentScript" in document; | |
document.write("<div><strong>Native currentScript supported?</strong> " + hasCSSupport + "</div>"); | |
showState('loadFirst'); | |
</script> | |
<script id="inlineScript"> | |
showState('inlineScript'); | |
var head = document.head || document.getElementsByTagName("head")[0]; | |
var newFirstScript = document.createElement("script"); | |
newFirstScript.id = "dynamicFirstScript"; | |
newFirstScript.text = "showState('dynamicFirstScript');"; | |
newFirstScript.async = false; | |
head.insertBefore(newFirstScript, head.firstChild); | |
</script> | |
<script id="loadLast"> | |
showState('loadLast'); | |
var newLastScript = document.createElement("script"); | |
newLastScript.id = "dynamicLastScript"; | |
newLastScript.text = "showState('dynamicLastScript');"; | |
newLastScript.async = false; | |
head = document.head || document.getElementsByTagName("head")[0]; | |
head.appendChild(newLastScript); | |
window.onload = function() { | |
showState("pageLoaded"); | |
setTimeout(function() { | |
var lateScript = document.createElement("script"); | |
lateScript.id = "lateScript"; | |
lateScript.text = "showState('lateScript');"; | |
lateScript.async = false; | |
var body = document.body || document.getElementsByTagName("body")[0]; | |
body.appendChild(lateScript); | |
}, 500); | |
}; | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment