Search code examples
javascripthtmlwinjs

WinJS Stop javascript in memory


I'm learning using the Win.JS navigation and I am able to navigate between pages now. But I'm having errors when I leave a page and trigger a event from the first page.

So the problem is. I have some javascript running on Page1.html | .js If I press the Arrow up button on my keyboard for example a javascript runs (onclick). This works okay, but then I click on a button and navigate to Page2.html also with a ( .js) and if I then also click on the Arrow up button I get an error. It tries to start the function that is called within the onclick, and that isn't there anymore in my Page2.

So the question is, how can I unload these javascript triggers from Page1 if i'm notttt on that page anymore?


Solution

  • In your WinJS Page definition you can define unload method that is always invoked when the user leaves the page. Unregister all event handlers there.

    WinJS.UI.Pages.define("/page.html", {
    
        ready: function (element, options) {
            // add onclick handler
        },
    
        unload: function() {
           // remove here all event handlers
        }
    
    });