How can I use create a Web worker from a string (which is supplied via a POST request)?
One way I can think of, but I’m not sure how to implement it, is by creating a data-URI from the server response, and passing that to the Worker constructor, but I’ve heard that some browsers don’t allow this, because of the same origin policy.
MDN states the uncertainty about the origin policy around data URI’s:
Note: The URI passed as parameter of the Worker constructor must obey the same-origin policy. There is currently disagreement among browsers vendors on whether data URIs are of the same-origin or not; Gecko 10.0 (Firefox 10.0 / Thunderbird 10.0) and later do allow data URIs as a valid script for workers. Other browsers may disagree.
Here’s also a post discussing it on the whatwg.
URL.createObjectURL(<Blob blob>)can be used to create a Web worker from a string. The blob can be created using theBlobBuilderAPI deprecated or theBlobconstructor.Demo: http://jsfiddle.net/uqcFM/49/
Compatibility
Web workers are supported in the following browsers source:
This method’s support is based on the support of the
BlobAPI and theURL.createObjectUrlmethod.Blobcompatibility:WebKitBlobBuilder), 20+ (Blobconstructor)MozBlobBuilder), 13+ (Blobconstructor)Blobconstructor)IE10 supports
MSBlobBuilderandURL.createObjectURL. However, trying to create a Web Worker from ablob:-URL throws a SecurityError.Opera 12 does not support
URLAPI. Some users may have a fake version of theURLobject, thanks to this hack inbrowser.js.Fallback 1: data-URI
Opera supports data-URIs as an argument to the
Workerconstructor. Note: Do not forget to escape special characters (Such as#and%).Demo: http://jsfiddle.net/uqcFM/37/
Fallback 2: Eval
evalcan be used as a fallback for Safari (<6) and IE 10.