Search code examples
javascripthtmlcssonsen-ui

javascript no working in <ons-template> - onsen ui


I want use JavaScript in many pages but it working only inside <ons-navigator> tag and don't working inside <ons-template>. You can see my live example jsfiddl and give me solution please. In the example when I scroll down I want show px position but its working only inside <ons-navigator>. How can I use JavaScript in <ons-template>?

Note: If you have another framework like onsen and working JavaScript in the cached page please write in a comment.


Solution

  • The problem you are experiencing is actually a bit different - it's just that your selector menu__menu doesn't match anything. Just add an id to the page which you want to listen for scroll events like

    <ons-page id="myPage">
    

    and then use

    var content = document.querySelector('#myPage .page__content');
    

    and you will be fine :)

    Fiddle

    Note: your side menu logic seems to be creating a new page after you switch the pages from there - if you want to execute some code each time when a page is initialized you may want to take a look at the init event - each page fires it when it becomes attached to dom. Docs