Javascript not working in Cordova with Onsen
-
I am trying to call a function in Javascript but its not working.
I didnt find any references on Onsen website and on Google as well.
Can any one tell me what are the Exact steps for calling a function in Javascript?
Do I need to define the function in index.js file or Can I write the function in the same file inside script tags?
I am using Onsen 2.
I had also asked a similar question regarding http://stackoverflow.com/questions/36932137/jquery-works-in-browser-but-not-in-an-android-emulator jquery but no solution yet.
p1.html
<body onload="onLoad()"> <ons-page> <ons-button id="demo" onclick="printMsg()">Click Me</ons-button> </ons-page>
index.js
function onLoad() { document.addEventListener("deviceready", onDeviceReady, false); } function onDeviceReady() { document.getElementById('demo').addEventListener("click", printMsg, false); document.getElementById('btnNext').addEventListener("click", callNextPage, false); document.addEventListener("pause", onPause, false); document.addEventListener("resume", onResume, false); document.addEventListener("menubutton", onMenuKeyDown, false); } function printMsg() { ons.notification.alert('Hello'): //alert('hello'); //document.addEventListener("deviceready", onDeviceReady, false); }
Output Error Msg:
“Uncaught ReferenceError: printMsg is not defined”, source: file:///android_asset/www/index.html
Edit :
I have Uploaded my Project at http://www.filedropper.com/onsenui_2
-
You can either write the code inside
<script>
tags or you can load it using:<script src="/path/to/script.js"></script>
Maybe you didn’t load
index.js
in your app. Can you try adding the following to your HTML?<script src="index.js"></script>
-
@argelius I have added the link for index.js file. You can view my project on the below link http://www.filedropper.com/onsenui_2
-
@Ashish I took a look at the code. I understand now what’s going on.
You should only put the
<ons-page>
element inside thep1.html
file since this is a template file.All scripts must be loaded in
index.html
so you should loadindex.js
together with the other javascript files.The
<script>
tags insidep1.html
is actually ignored since they are not executed if they are dynamically injected into the page.The app looks great! Hope you will be able to solve your issues.
-
@argelius Hey! We got it at the same time. I responded on SOF the same. LOL! @Ashish , @argelius is correct your logic should be contained in the index.html file.
-
This post is deleted!
-
Got the Result , Finally I saw the Alert Box in my Emulator…Thanks for your suggestions @argelius and @munsterlander .
-
I just wanted to know why I am not able to use ons.notification.alert() whereas simple alert() is working properly