locked
Disposing Ads for multi-page app

    Question

  • Hello,

    I am making a mulit-paged app using the Navigation template. I am trying to incorporate an ad on each page, so every time I navigate to a new page, I am disposing the ad using the "onnavigating" event of WinJS.Navigation. When I navigate to a new page, and press the back button, I am getting an error:

    Unhandled exception at line 1494, column 21 in ms-appx://5cb987c2-024d-4e79-b469-68bad1bad808/MSAdvertisingJS/ads/ad.js
    0x800a138f - JavaScript runtime error: Unable to get property 'offsetWidth' of undefined or null reference

    Here is some sample code of what I am trying to do:

    WinJS.UI.Pages.define("/pages/home/home.html", { ready: function (element, options) { var Btn_Login = document.getElementById('Btn_Login'); Btn_Login.addEventListener('click', function () { WinJS.Navigation.navigate('/pages/page2/page.html'); }, false);

    function disposeAds() { console.log('navigating'); ad1.winControl.dispose(); WinJS.Navigation.removeEventListener('navigating', disposeAds, false); }; WinJS.Navigation.addEventListener('navigating', disposeAds, false);

    I dispose the ad the same way on the other page. 

    Any help would be appreciated :)

    Wednesday, August 06, 2014 3:53 AM

Answers

  • I found my mistake, I had to use the "beforenavigate" event instead of "navigating". Using the navigating event, the page already changed before I could dispose the ads, so the "beforenavigate" event was more appropriate.
    • Marked as answer by datQkiD2 Wednesday, August 06, 2014 4:45 PM
    Wednesday, August 06, 2014 4:45 PM

All replies

  • I found my mistake, I had to use the "beforenavigate" event instead of "navigating". Using the navigating event, the page already changed before I could dispose the ads, so the "beforenavigate" event was more appropriate.
    • Marked as answer by datQkiD2 Wednesday, August 06, 2014 4:45 PM
    Wednesday, August 06, 2014 4:45 PM
  • That's true, you have do it with onbeforenavigate event.

    Thanks for sharing your solution.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 07, 2014 1:48 AM
    Moderator