Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7857563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:04:42+00:00 2026-06-02T21:04:42+00:00

How can I use create a Web worker from a string (which is supplied

  • 0

How can I use create a Web worker from a string (which is supplied via a POST request)?

One way I can think of, but I’m not sure how to implement it, is by creating a data-URI from the server response, and passing that to the Worker constructor, but I’ve heard that some browsers don’t allow this, because of the same origin policy.

MDN states the uncertainty about the origin policy around data URI’s:

Note: The URI passed as parameter of the Worker constructor must obey the same-origin policy. There is currently disagreement among browsers vendors on whether data URIs are of the same-origin or not; Gecko 10.0 (Firefox 10.0 / Thunderbird 10.0) and later do allow data URIs as a valid script for workers. Other browsers may disagree.

Here’s also a post discussing it on the whatwg.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:04:43+00:00Added an answer on June 2, 2026 at 9:04 pm

    Summary

    • blob: for Chrome 8+, Firefox 6+, Safari 6.0+, Opera 15+
    • data:application/javascript for Opera 10.60 – 12
    • eval otherwise (IE 10+)

    URL.createObjectURL(<Blob blob>) can be used to create a Web worker from a string. The blob can be created using the BlobBuilder API deprecated or the Blob constructor.

    Demo: http://jsfiddle.net/uqcFM/49/

    // URL.createObjectURL
    window.URL = window.URL || window.webkitURL;
    
    // "Server response", used in all examples
    var response = "self.onmessage=function(e){postMessage('Worker: '+e.data);}";
    
    var blob;
    try {
        blob = new Blob([response], {type: 'application/javascript'});
    } catch (e) { // Backwards-compatibility
        window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
        blob = new BlobBuilder();
        blob.append(response);
        blob = blob.getBlob();
    }
    var worker = new Worker(URL.createObjectURL(blob));
    
    // Test, used in all examples:
    worker.onmessage = function(e) {
        alert('Response: ' + e.data);
    };
    worker.postMessage('Test');
    

    Compatibility

    Web workers are supported in the following browsers source:

    • Chrome 3
    • Firefox 3.5
    • IE 10
    • Opera 10.60
    • Safari 4

    This method’s support is based on the support of the Blob API and the URL.createObjectUrl method. Blob compatibility:

    • Chrome 8+ (WebKitBlobBuilder), 20+ (Blob constructor)
    • Firefox 6+ (MozBlobBuilder), 13+ (Blob constructor)
    • Safari 6+ (Blob constructor)

    IE10 supports MSBlobBuilder and URL.createObjectURL. However, trying to create a Web Worker from a blob:-URL throws a SecurityError.

    Opera 12 does not support URL API. Some users may have a fake version of the URL object, thanks to this hack in browser.js.

    Fallback 1: data-URI

    Opera supports data-URIs as an argument to the Worker constructor. Note: Do not forget to escape special characters (Such as # and %).

    // response as defined in the first example
    var worker = new Worker('data:application/javascript,' +
                            encodeURIComponent(response) );
    // ... Test as defined in the first example
    

    Demo: http://jsfiddle.net/uqcFM/37/

    Fallback 2: Eval

    eval can be used as a fallback for Safari (<6) and IE 10.

    // Worker-helper.js
    self.onmessage = function(e) {
        self.onmessage = null; // Clean-up
        eval(e.data);
    };
    // Usage:
    var worker = new Worker('Worker-helper.js');
    // `response` as defined in the first example
    worker.postMessage(response);
    // .. Test as defined in the first example
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any tools using which I can use to create Windows forms from
For laying out a long string into pages, I can use CTFramesetterCreateFrame to create
I testing setting up a web logic server which can use an Oracle AQ
I'm preparing to create a WCF Service which our customers can use to update
Is there any library out there that I can use to create epub files
Is there a pattern, Xml structure, architecture technique we can use to create simple
What install tool can I use to create Virtual Directory on IIS? OpenSource, free
If we can use pointers and malloc to create and use arrays, why does
I recently discovered that I can use lambdas to create simple event handlers. I
I annotated a bunch of POJO's so JPA can use them to create tables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.