locked
Jquery in background task JavaScript runtime error: 'window' is undefined

    Question

  • How do we access the window variable in backgroundtasks ?

    i need to use jquery and i get  JavaScript runtime error: 'window' is undefined

    Monday, January 14, 2013 1:59 PM

Answers

  • It looks like jQuery is not written to be used from a web worker--or else you need a variant that is. This is because the global context in a background task written in JavaScript is WorkerGlobalScope (see http://www.w3.org/TR/workers/#workerglobalscope) not window, so window isn't defined.

    You might be able to work around a situation like this, depending on what is needed from window. (I'm assuming jQuery is trying to initialize its UI libraries, in which case you probably won't be able to do that from a worker/background task. They can't do UI directly.)

    If you need something from window, specifically, your running app, when it registers the background task, could also write the necessary values into the Windows.Storage.ApplicationData.localSettings container as background tasks can access that at any time.

    It could be possible, then, to write the necessary values from window into local settings, then create a window object and populate it with those values before loading jQuery. It'd be a hack, but might get you where you need to be.

    That said, because there are limitations on what you can do from a background task, and how long you can run, you might ask whether jQuery is really buying you anything in that context. I can't say, of course, but it's a reasonable question to ask before trying this kind of workaround.

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press


    Monday, January 14, 2013 11:16 PM
  • A Background Task can be running when there is no window (the App has been swapped out) hence referencing the window from the task might not be the best solution.

    You could subscribe to a custom event with a handler that does what you need on the window and from the background task fire the event.

    If the App is running, there will be a listener active, if the App is swapped out, there will be none, no errors will occur and the "window" will be accesed only when it's really there.

    This solution might work depending on what exactly are you trying to do with the window.

    //subscribe listener
    WinJS.Application.addEventListener("mycustomevent", function (e) { console.log("Event Rised"); /*access window*/ });
    
    //fire event from task using:
    WinJS.Application.queueEvent({type: "mycustomevent"})

    Monday, January 14, 2013 7:59 PM

All replies

  • A Background Task can be running when there is no window (the App has been swapped out) hence referencing the window from the task might not be the best solution.

    You could subscribe to a custom event with a handler that does what you need on the window and from the background task fire the event.

    If the App is running, there will be a listener active, if the App is swapped out, there will be none, no errors will occur and the "window" will be accesed only when it's really there.

    This solution might work depending on what exactly are you trying to do with the window.

    //subscribe listener
    WinJS.Application.addEventListener("mycustomevent", function (e) { console.log("Event Rised"); /*access window*/ });
    
    //fire event from task using:
    WinJS.Application.queueEvent({type: "mycustomevent"})

    Monday, January 14, 2013 7:59 PM
  • It looks like jQuery is not written to be used from a web worker--or else you need a variant that is. This is because the global context in a background task written in JavaScript is WorkerGlobalScope (see http://www.w3.org/TR/workers/#workerglobalscope) not window, so window isn't defined.

    You might be able to work around a situation like this, depending on what is needed from window. (I'm assuming jQuery is trying to initialize its UI libraries, in which case you probably won't be able to do that from a worker/background task. They can't do UI directly.)

    If you need something from window, specifically, your running app, when it registers the background task, could also write the necessary values into the Windows.Storage.ApplicationData.localSettings container as background tasks can access that at any time.

    It could be possible, then, to write the necessary values from window into local settings, then create a window object and populate it with those values before loading jQuery. It'd be a hack, but might get you where you need to be.

    That said, because there are limitations on what you can do from a background task, and how long you can run, you might ask whether jQuery is really buying you anything in that context. I can't say, of course, but it's a reasonable question to ask before trying this kind of workaround.

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press


    Monday, January 14, 2013 11:16 PM
  • Monday, January 21, 2013 4:16 PM
  • Hi Ashley,

    Do you have any sample code /example how you solved the issue.

    I am new to js/visual studio.Any help would be great.

    Thanks

    Saturday, April 11, 2015 11:10 PM