Closed wilsonpage closed 9 years ago
var src = 'my-service.js';
var base = document.location.origin;
var html = '<script src="' + base + '/threads.js"><\/script><script src="' + base + '/' + src + '"><\/script>';
var blob = new Blob([html], {type: "text/html"});
var iframe = document.createElement('iframe');
iframe.src = URL.createObjectURL(blob);
document.body.appendChild(iframe);
What about the following?
iframe.src = 'data:text/html;utf-8,<!DOCTYPE html><script defer src="' + base + '/threads.js"><script defer src="' + base + '/' + src + '"></script>';
Also, note the defer
attribute.
Nice! Even simpler! I had no idea you could write data-uris like that!
After some discussion with @arcturus we've decided that the for reasons of limiting 'magic' theads.js
should not be auto-loaded into the window. It seems like it would be a slippery slope to adding a lot of script loading cruft to the library.
I wrote a gist to illustrate how it's possible to run the same service in a Worker
or a Window
.
Closing this. Willing to reopen if this proves to be valuable.
Currently users have to define
.html
sources for 'window' type threads. This is not ideal as it means that scripts can't be moved between thread types easily.Open questions:
importScripts
vs async script loading). How can users author once and run anywhere? Requirejs can run in both window and worker context, so this might be handy.Blob
?