Say my html file is from http://foo.com/index.html, in it, there's a <script>
tag to http://bar.com/bar.js. In bar.js, I want to start a开发者_JAVA百科 SharedWorker where the url is http://bar.com/worker.js. Is there a way to achieve this (maybe something like jsonp)?
The preferred way to do this sort of cross-domain access these days is using the W3 CORS specification.
Cross-Origin Resource Sharing
However, this might not be suitable for you if you do not control the the site at bar.com. If you do, then CORS is definitely a good option, but you may need to resort to JSONP if bar.com is run by another party, since CORS depends on the site sending back specific headers authorizing your browser to download the resource you requested.
This is a solution I found:
- Write the script inside a function (can be an inner function)
- get the text using function.toString() (removing the function declaration and closing brace)
- append the text to a BlobBuilder and get the blob
- Use window.URL.createObjectURL to convert the blob to a url
- use that url for the worker
精彩评论