Advertisement
Guest User

Untitled

a guest
Jul 21st, 2015
1,864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Sandbox</title>
  5.         <meta charset="UTF-8">
  6.         <link rel="stylesheet" href="/JSDev/styles.css">
  7.         <script>
  8.             function loadScript(location, runOnLoad) {
  9.                 // Check for existing script element and delete it if it exists
  10.                 var js = document.getElementById("sandboxScript");
  11.                 if(js) {
  12.                     document.head.removeChild(js);
  13.                     sandbox = undefined;
  14.                     console.info("---------- Script refreshed ----------");
  15.                 }
  16.                
  17.                 // Create new script element and load a script into it
  18.                 js = document.createElement("script");
  19.                 document.head.appendChild(js);
  20.                 if(runOnLoad)
  21.                     js.onload = function() { sandbox(); };
  22.                 js.src = location;
  23.                 js.id = "sandboxScript";
  24.             }
  25.            
  26.             // Make sure script has loaded and display an alert if it has not
  27.             function runScript() {
  28.                 if(typeof sandbox !== "undefined") {
  29.                     sandbox();
  30.                 } else {
  31.                     window.alert("The script has not been loaded yet!");
  32.                 }
  33.             }
  34.         </script>
  35.     </head>
  36.     <body onload="loadScript('sandbox.js')">
  37.         <h1>Sandbox</h1>
  38.         <p><em><a href="/JSDev/">Return to home page</a></em></p>
  39.         <button id="refreshButton" onclick="loadScript('sandbox.js')">
  40.             Refresh script
  41.         </button>
  42.         <button id="runButton" onclick="runScript()">
  43.             Run script
  44.         </button>
  45.         <br>
  46.         <button id="refreshRunButton" style="font-weight:bold"
  47.                onclick="loadScript('sandbox.js', true)">
  48.             Refresh and run script
  49.         </button>
  50.     </body>
  51. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement