Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access jQuery in HTML 5 web worker

Tags:

I am unable to to access jQuery inside an HTML5 web worker. Is there a way I can do that?

like image 382
Imran Qadir Baksh - Baloch Avatar asked May 08 '12 01:05

Imran Qadir Baksh - Baloch


People also ask

Can you put jQuery in HTML?

jQuery is a JavaScript file that you will link to in your HTML. There are two ways to include jQuery in a project, which is to download a local copy or link to a file via Content Delivery Network (CDN).

Where do I put jQuery file in HTML?

Include the jQuery by CDN Step 1: Firstly, we have to open that Html file in which we want to add the jQuery using CDN. Step 2: After then, we have to place the cursor between the head tag just before the title tag. And, then we have to use the <script> tag, which specify the src attribute for adding.

Which of the following is not accessible to html5 web worker?

Since web workers are in external files, they do not have access to the following JavaScript objects: The window object. The document object. The parent object.

What is JS web worker API in html5?

Web Workers API A worker is an object created using a constructor (e.g. Worker() ) that runs a named JavaScript file — this file contains the code that will run in the worker thread; workers run in another global context that is different from the current window .


2 Answers

tl;dr: include this script before jQuery

JQuery initally accesses lots of document properties to check for browser features. document is not defined in Worker and you actually cannot create Document instance in web workers at this moment. JQuery isn't prepared for that - as you could see in comments on your question, nobody seems to understand what would you do with JQuery without DOM.

Since, as I said, JQuery needs document to initialise, I created a dummy fake document object. This object acts as real document during JQuery tests:

var document = self.document = {parentNode: null, nodeType: 9, toString: function() {return "FakeDocument"}}; var window = self.window = self; var fakeElement = Object.create(document); fakeElement.nodeType = 1; fakeElement.toString=function() {return "FakeElement"}; fakeElement.parentNode = fakeElement.firstChild = fakeElement.lastChild = fakeElement; fakeElement.ownerDocument = document;  document.head = document.body = fakeElement; document.ownerDocument = document.documentElement = document; document.getElementById = document.createElement = function() {return fakeElement;}; document.createDocumentFragment = function() {return this;}; document.getElementsByTagName = document.getElementsByClassName = function() {return [fakeElement];}; document.getAttribute = document.setAttribute = document.removeChild =    document.addEventListener = document.removeEventListener =    function() {return null;}; document.cloneNode = document.appendChild = function() {return this;}; document.appendChild = function(child) {return child;}; 

Beware that this fake document is only meant to allow jQuery to load, it won't allow any real DOM operations.

Example usage:

importScripts("workerFakeDOM.js"); importScripts('jquery-2.1.4.min.js'); console.log("JQuery version:", $.fn.jquery); 

Test script

Allows you to try various versions of JQuery with my script.

JSfiddle

Check that you're using http://, my domain doesn't work with https://.

Download as script

like image 84
Tomáš Zato - Reinstate Monica Avatar answered Sep 20 '22 18:09

Tomáš Zato - Reinstate Monica


The answer of Tomáš Zato was correct, but no longer works with newer jQuery version. I added some more for jQuery 3.1.1:

var document = self.document = { parentNode: null, nodeType: 9, toString: function () { return "FakeDocument" } }; var window = self.window = self; var fakeElement = Object.create(document); fakeElement.nodeType = 1; fakeElement.toString = function () { return "FakeElement" }; fakeElement.parentNode = fakeElement.firstChild = fakeElement.lastChild = fakeElement; fakeElement.ownerDocument = document;  document.head = document.body = fakeElement; document.ownerDocument = document.documentElement = document; document.getElementById = document.createElement = function () { return fakeElement; }; document.createDocumentFragment = function () { return this; }; document.getElementsByTagName = document.getElementsByClassName = function () { return [fakeElement]; }; document.getAttribute = document.setAttribute = document.removeChild =     document.addEventListener = document.removeEventListener =     function () { return null; }; document.cloneNode = document.appendChild = function () { return this; }; document.appendChild = function (child) { return child; }; document.childNodes = []; document.implementation = {     createHTMLDocument: function () { return document; } } 
like image 20
Luke Vo Avatar answered Sep 21 '22 18:09

Luke Vo