-
-
Save amcdnl/3822982 to your computer and use it in GitHub Desktop.
Inline Web Workers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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