Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Forked from bemson/index.html
Last active December 5, 2023 07:14
Show Gist options
  • Save nolanlawson/23eff93d27ad09ff44b7e4d56ffd1d54 to your computer and use it in GitHub Desktop.
Save nolanlawson/23eff93d27ad09ff44b7e4d56ffd1d54 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