cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <html>
  <body>
    <center><h1>Widget Maker</h1></center>
    <div id="widget-holder">
      <p><label>Widget</label></p>
      <div id="widget"></div>
    </div>
    <div id="code">
      <p><label>Widget code</label></p>
      <p><textarea id="codebox" cols="40" rows="10">$().html("<h1>Hello Widget World!</h1>");
setTimeout(function() {
  $("h1").text("Goodbye Widget World!");
}, 3000);</textarea></p>
      <p><button type="submit" id="run-button">Run Widget</button></p>
    </div>
  </body>
</html>
            
          
!
            
              body {
  font-family: Helvetica, Verdana, Arial, sans-serif;
  font-size: 14px;
  background-color: #7FDBFF;
  color: #001F3F;
  max-width: 720px;
}

h1, label, button {
  text-transform: uppercase;
}

label {
  color: #001F3F;
  font-weight: bold;
}

button {
  background-color: #001F3F;
  color: white;
  border: none;
  padding: 20px;
  cursor: pointer;
}

#code {
  margin-right: 380px;
}

#codebox {
  font-size: 14px;
  border: none;
  margin: 0px;
  padding: 8px;
  font-family: monospace;
}

#widget-holder {
  float: right;
  width: 350px;
  height: 150px;
  margin-top: -14px;
}

#widget {
  padding: 8px;
  background-color: white;
  width: 100%;
  height: 100%;
}
            
          
!
            
              /* Used as the worker prelude - runs in the worker context */
function setupWorker(widgetFunc) {
    
  var postMessage = self.postMessage;
    
  /* Constructs a view manipulation object */
  function view(id) {
    function updateView(action, content) {
      postMessage.call(self, {
        id: id,
        action: action,
        content: content
      }); 
    }
    
    return {
      html: function(val) {
        updateView("html", val);
      },
      text: function(val) {
        updateView("text", val);
      }
    };
  }
  
  self.$ = view;
  
  // remove widget code access to postMessage
  self.postMessage = null;
  
  // freeze context so widget code can't change it
  Object.freeze(self);
  
  // run the widget's code
  widgetFunc();
}

/* ----------------------------------------------------- */

/*
 * Create the code to run in the web worker.
 * Use the contents of the setupWorker function concatenated
 * with the widget code wrapped in a function.
 */
function createCode() {
  var widgetCode = $("#codebox").val();
  var code = setupWorker.toString() +
      "\nfunction widgetCode() {" + widgetCode + "}" +
      "\nsetupWorker(widgetCode);";
  return code;
}

/*
 * Handle messages from the worker relating to updating the
 * widget's view content.
 */
function handleMessage(msg) {
  var data = msg.data;
  var content = html_sanitize(data.content);
  var sel = "#widget";
  if(data.id != null) sel += " " + data.id;
  
  if(data.action == 'html') {
    $(sel).html(content);  
  } else if(data.action == 'text') {
    $(sel).text(content);
  } else {
    content.warn("Unrecognized action: %s", data.action);
  }
}

function runWorker() {  
  console.clear();
  var code = createCode();
    
  // Needs support for Blob (IE10+)
  var blob = new Blob([code], { type: 'application/javascript' });

  var worker = new Worker(URL.createObjectURL(blob));
  worker.onmessage = handleMessage;
}

$(document).ready(function() {
  console.clear();
  $("#run-button").click(runWorker);
});
            
          
!
999px
Loading ..................

Console