0

enter image description here

I'm trying to link to my worker file from the main script but I get an error 404 not found on the console. The worker file main scripts are in the same directory. What could I be doing wrong ?

const myworker = new Worker('worker.js');

function checkZipCode() {
    event.preventDefault();

    let zipcode = document.getElementById('zipcode');

    myworker.postMessage(zipcode.value);
    console.log('Message posted to worker')
};

// Worker.js file

addEventListener('message', (e) => {
    console.log(e)
})
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 2
    Possible duplicate of [How do I use javascript require?](https://stackoverflow.com/questions/43468826/how-do-i-use-javascript-require) – AZ_ Aug 18 '19 at 17:57
  • Are you using this backend or frontend? look into `require` and `import` for an understanding of how to factor files and modules. – aviya.developer Aug 18 '19 at 17:58
  • I think it's just a path issue, try `new Worker('./worker.js');` – James South Aug 18 '19 at 18:10
  • @JamesSouth I have tried ('./worker.js); I still get same response – alexander Udose Aug 18 '19 at 18:18
  • you also have `worker.js` and then the comment below is `Worker.js`. otherwise, looks ok – James South Aug 18 '19 at 18:23
  • If this is a path issue its very confusing. The worker.js file and the main.js files are in the same directory. – alexander Udose Aug 18 '19 at 18:32
  • is the worker file called worker or Worker? also see yesterday's answer: https://stackoverflow.com/questions/57532356/web-worker-integration/57532963#57532963 – James South Aug 18 '19 at 18:42
  • Well if the console shows a 404 error then it should also should the complete URL of the file that it tried to load. Put that url in your browser address bar to see if it works. If it (likely) doesn't, check whether it's the right url and your server is misconfigured so that it doesn't serve the file, or whether it is the wrong url. Tell us the results of this. – Bergi Aug 18 '19 at 19:28

0 Answers0