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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console