Created
August 11, 2014 03:49
-
-
Save jtyjty99999/a730a17258fca04bfca3 to your computer and use it in GitHub Desktop.
跨域webworker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function XHRWorker(url, ready, scope) { | |
var oReq = new XMLHttpRequest(); | |
oReq.addEventListener('load', function() { | |
var worker = new Worker(window.URL.createObjectURL(new Blob([this.responseText]))); | |
if (ready) { | |
ready.call(scope, worker); | |
} | |
}, oReq); | |
oReq.open("get", url, true); | |
oReq.send(); | |
} | |
function WorkerStart() { | |
XHRWorker("http://static.xxx.com/js/worker.js", function(worker) { | |
worker.postMessage("hello world"); | |
worker.onmessage = function(e) { | |
console.log(e.data); | |
} | |
}, this); | |
} | |
WorkerStart(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there, I have tried your method but chrome responds with Access to XMLHttpRequest at 'xxx' from origin 'xx' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Please let me know how to solve it. Thanks!
BTW The url I am using starts with https, if that is the case.