Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Created January 13, 2012 21:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JeffreyWay/1608901 to your computer and use it in GitHub Desktop.
Save JeffreyWay/1608901 to your computer and use it in GitHub Desktop.
Inline Web Workers
<!doctype html>
<html>
<head>
<title>Web Workers</title>
</head>
<body>
<script id="worker" type="app/worker">
addEventListener('message', function() {
postMessage('What up, sucka.');
}, false);
</script>
<script>
(function() {
if ( !window.BlobBuilder ) {
window.BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder;
}
if ( !window.URL ) {
window.URL = window.webkitURL || window.mozURL;
}
if ( !window.BlobBuilder || !window.URL || !window.Worker ) {
return;
// or implement polyfill...probably not necessary though
}
var blob = new BlobBuilder(),
url,
worker;
blob.append(
document.querySelector('#worker').textContent
);
url = window.URL.createObjectURL( blob.getBlob() );
worker = new Worker( url );
worker.addEventListener('message', function(e) {
console.log(e.data); // What up, sucka.
}, false);
worker.postMessage('');
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment