How to create a web worker from a script from a subdomain?

I have a website at example.com and I serve all external resources from cdn.example.com . So, on my HTML page in example.com , I have something like:

 <script type="text/javascript" src="http://cdn.example.com/script.js"></script> 

In my script, I want to create a web worker, so I:

 worker = new Worker("http://cdn.example.com/script.js"); 

But this fails in Firefox 16 with the error Failed to load script: http://cdn.example.com/script.js (nsresult = 0x805303f4) . It works on Safari 6 and Chrome 22.

The problem seems to be that the origin is different . The effective start of the script is example.com and cdn.example.com does not match this. This seems like a mistake because CORS doesn't help without setting document.domain (or at least I couldn't get it to work while playing with it). Is there any way to make it work?

+4
source share
1 answer

In the end, I made a website at example.com for simple JavaScript code that contains real web code:

 importScripts('http://cdn.example.com/script.js'); 

and then create a web artist by pointing it out.

+2
source

Source: https://habr.com/ru/post/1442626/


All Articles