Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: window is not defined

importScripts('js/jquery.js');
importScripts('js/jquery.mobile-1.2.0.js');
importScripts('cordova.js');
importScripts('DataBase.js');
importScripts('SaveData.js');

self.addEventListener('message', function(e) {
    queryDB(function(arr) {

        self.postMessage(e.data + arr);

    });


}, false);

here i tried to retrieve data from database and show in parent page using html 5 worker thread.But i get following error.

Uncaught ReferenceError: window is not defined 
like image 687
user2889058 Avatar asked Feb 06 '26 21:02

user2889058


2 Answers

You cannot importScript jQuery, because jQuery requires DOM access, which web workers don't have.

like image 176
Oracle Avatar answered Feb 09 '26 11:02

Oracle


If you experience this error with Web Workers using importScripts functionality, it's because the script you're importing may be referencing window object; this is not supported in Web Workers.

To fix this, do the following:

  1. Put this code before any other code in the imported script:
var _window = this || self || window;

The above ensures that self (which is compatible with Web Workers) is called before unsupported window.

  1. Replace all occurrences of window with _window.
like image 27
ObiHill Avatar answered Feb 09 '26 11:02

ObiHill