Skip to content

Instantly share code, notes, and snippets.

@calvinmetcalf
Created May 21, 2013 01:38
Show Gist options
  • Save calvinmetcalf/5616996 to your computer and use it in GitHub Desktop.
Save calvinmetcalf/5616996 to your computer and use it in GitHub Desktop.
Worker urls are resolved relative to the HTML not the script, here's how to write a script that can be called as js/scriptname.js and as scripts/scriptname.js. Still has to be same domain.
if (typeof document === "undefined") {(function(self){
self.onmessage=function(e){
/*
worker stuff
*/
}
})(self)} else {(function(exports){
"use strict";
function getPath(){
var scripts = document.getElementsByTagName("script");
var len = scripts.length;
var i = 0;
while(i<len){
/* change --------> */if(/scriptWithWorker(\.min)?\.js/.test(scripts[i].src)){
return scripts[i].src;
}
i++;
}
}
var worker = new Worker(getPath());
/*
none worker code
*/
})(window)};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment