Skip to content

Instantly share code, notes, and snippets.

@stickupkid
Created April 4, 2012 09:14
Show Gist options
  • Save stickupkid/2299889 to your computer and use it in GitHub Desktop.
Save stickupkid/2299889 to your computer and use it in GitHub Desktop.
Example of a blob builder.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="log"></div>
<script id="worker1" type="javascript/worker">
// This script won't be parsed by JS engines because its type is javascript/worker.
self.onmessage = function(e) {
self.postMessage('msg from worker 1');
};
// Rest of your worker code goes here.
</script>
<script id="worker2" type="javascript/worker">
// This script won't be parsed by JS engines because its type is javascript/worker.
self.onmessage = function(e) {
self.postMessage('msg from worker 2');
};
// Rest of your worker code goes here.
</script>
<script>
function log(msg) {
// Use a fragment: browser will only render/reflow once.
var fragment = document.createDocumentFragment();
fragment.appendChild(document.createTextNode(msg));
fragment.appendChild(document.createElement('br'));
document.querySelector("#log").appendChild(fragment);
}
var bb = new BlobBuilder();
bb.append(document.querySelector('#worker1').textContent);
bb.append(document.querySelector('#worker2').textContent);
// Note: window.webkitURL.createObjectURL() in Chrome 10+.
var worker = new Worker(window.URL.createObjectURL(bb.getBlob()));
worker.onmessage = function(e) {
log("Received: " + e.data);
}
worker.postMessage(); // Start the worker.
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment