-
-
Save nolanlawson/23eff93d27ad09ff44b7e4d56ffd1d54 to your computer and use it in GitHub Desktop.
Web Worker via blob URL
This file contains 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 lang="en"> | |
<body> | |
<span id="output"></span> | |
</body> | |
<script> | |
(function () { | |
var workerBlob = new Blob( | |
[workerRunner.toString().replace(/^function .+\{?|\}$/g, '')], | |
{ type:'text/javascript' } | |
); | |
var workerBlobUrl = URL.createObjectURL(workerBlob); | |
var worker = new Worker(workerBlobUrl); | |
worker.onmessage = function(event) { | |
output.textContent = 'Output is: ' + event.data; | |
}; | |
worker.postMessage('foo'); | |
function workerRunner() { | |
self.onmessage = function(event) { | |
self.postMessage('launched worker via blob URL!'); | |
} | |
}; | |
})(); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment