Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript not working in Pushed Page of ons-page Onsen UI

I'm using Cordova/PhoneGap and Onsen UI. I have two html pages. Switch(pushpage) button is working perfect. But Javascript not working in search.html

Here is my code:

  • index.html (Default Main Page)
  • search.html

index.html

...
<body>

<ons-navigator title="Navigator" var="myNavigator">
<ons-button
        onclick="myNavigator.pushPage('search.html', { animation: 'lift' })">
        Switch Page
</ons-button>
</ons-navigator>
</body>

search.html

<ons-page>
<script>
alert('Testing Javascript');
</script>
</ons-page>
like image 795
user1999 Avatar asked Nov 28 '25 10:11

user1999


1 Answers

The search.html page is inserted dynamically when myNavigator.pushPage('search.html') is called.

When a <script> tag is inserted in this way the code is not executed.

Please refer to this question for more info: Can scripts be inserted with innerHTML?

In order to execute some script when a page is pushed you can use the postpush event:

ons.ready(function() {
  myNavigator.on('postpush', function() {
    alert('Hello, world!');
  });
});
like image 158
Andreas Argelius Avatar answered Nov 29 '25 23:11

Andreas Argelius