Created
January 16, 2018 22:13
-
-
Save bwDraco/c9c5b6843a567e84dea201fb603ffb9e to your computer and use it in GitHub Desktop.
JavaScript sandbox with dynamic loading for rapid testing of code
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Sandbox</title> | |
<meta charset="UTF-8"> | |
<link rel="stylesheet" href="/JSDev/styles.css"> | |
<script> | |
function loadScript(location, runOnLoad) { | |
// Check for existing script element and delete it if it exists | |
var js = document.getElementById("sandboxScript"); | |
if(js) { | |
document.head.removeChild(js); | |
sandbox = undefined; | |
console.info("---------- Script refreshed ----------"); | |
} | |
// Create new script element and load a script into it | |
js = document.createElement("script"); | |
document.head.appendChild(js); | |
if(runOnLoad) | |
js.onload = function() { sandbox(); }; | |
js.src = location; | |
js.id = "sandboxScript"; | |
} | |
// Make sure script has loaded and display an alert if it has not | |
function runScript() { | |
if(typeof sandbox !== "undefined") { | |
sandbox(); | |
} else { | |
window.alert("The script has not been loaded yet!"); | |
} | |
} | |
</script> | |
</head> | |
<body onload="loadScript('sandbox.js')"> | |
<h1>Sandbox</h1> | |
<p><em><a href="/JSDev/">Return to home page</a></em></p> | |
<button id="refreshButton" onclick="loadScript('sandbox.js')"> | |
Refresh script | |
</button> | |
<button id="runButton" onclick="runScript()"> | |
Run script | |
</button> | |
<br> | |
<button id="refreshRunButton" style="font-weight:bold" | |
onclick="loadScript('sandbox.js', true)"> | |
Refresh and run script | |
</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment