Skip to content

Instantly share code, notes, and snippets.

@loganpowell
Forked from nolanlawson/index.html
Created September 6, 2019 14:18
Show Gist options
  • Save loganpowell/b540a092df1d4d94ce08587833b29a2f to your computer and use it in GitHub Desktop.
Save loganpowell/b540a092df1d4d94ce08587833b29a2f to your computer and use it in GitHub Desktop.
Web Worker via blob URL
<!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