Reputation: 493
I got some script that seem to not be executed but I don't know why, I'm trying to debug this with the IOS simulator and safari to see the log.
Here is the content of the index.html
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Test</title>
</head>
<body>
<div id="appContainer" class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script>
alert('Wait!'); // is not working
</script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
and here is the content of the index.js
alert("wait"); // That one is working, why ?
var app = {
// Application Constructor
initialize: function() {
console.log("app init"); // not working
this.bindEvents();
console.log("app init end"); // not working
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// "load", "deviceready", "offline", and "online".
bindEvents: function() {
console.log("bind start"); // not working
document.addEventListener("deviceready", this.onDeviceReady, false);
document.addEventListener("click", this.onClick, false);
console.log("bind end"); // not working
},
// deviceready Event Handler
//
// The scope of "this" is the event. In order to call the "receivedEvent"
// function, we must explicitly call "app.receivedEvent(...);"
onDeviceReady: function() {
console.log("device start");
app.receivedEvent("deviceready");
document.getElementById("appContainer").addEventListener("click", this.onClick, false);
document.getElementById("appContainer").addEventListener("touchstart", this.onClick, false);
document.getElementById("appContainer").addEventListener("touchend", this.onClick, false);
console.log("device end");
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector(".listening");
var receivedElement = parentElement.querySelector(".received");
listeningElement.setAttribute("style", "display:none;");
receivedElement.setAttribute("style", "display:block;");
console.log("Received Event: " + id);
},
onClick: function(e) {
console.log("click"); // not working or it may be the eventlistener
},
};
console.log("init s"); // not working
app.initialize();
console.log("init e"); // not working
What I dont understand is 1 - Why the alert("Wait") in index.html is not working. 2 - Why the console.log doesn't work execpt the device start, device end and Received Event: deviceready 3 - Why the app.initialize don't seem to be executed (because there are no log) but is actually executed because the device ready event is fired 4 - Why can't I see the log in the onClick function when I trigger a click in the simulator (if the device ready event work, click, touchstart and touchend should work no ?)
Can somebody explain to me why nothing seems to work properly ?
I'm using cordova 5.3.1 with almost no modification from the init package. I tried to remove ios and add it again but it doesn't change anything.
Thank for the help
Upvotes: 0
Views: 2084
Reputation: 1220
The "console" global is an Apache Cordova plugin. It will only be available ondeviceready. Have a look here http://plugins.cordova.io/#/package/org.apache.cordova.console
Try this
var app = {
initialize: function() {
console.log("app init");
this.bindEvents();
console.log("app init end");
},
// Bind Event Listeners
bindEvents: function() {
console.log("bind start");
document.addEventListener("click", this.onClick, false);
console.log("bind end");
},
onDeviceReady: function() {
console.log("device start");
app.initialize();
app.receivedEvent("deviceready");
document.getElementById("appContainer").addEventListener("click", this.onClick, false);
document.getElementById("appContainer").addEventListener("touchstart", this.onClick, false);
document.getElementById("appContainer").addEventListener("touchend", this.onClick, false);
console.log("device end");
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector(".listening");
var receivedElement = parentElement.querySelector(".received");
listeningElement.setAttribute("style", "display:none;");
receivedElement.setAttribute("style", "display:block;");
console.log("Received Event: " + id);
},
onClick: function(e) {
console.log("click"); // not working or it may be the eventlistener
},
};
//initialize app on device ready
document.addEventListener("deviceready", app.onDeviceReady, false);
As for your "alert" mystery, window.alert compatibility has been proven unreliable in the context of Cordova. I advise you to use
navigator.notification.alert
Which is part of plugin-dialogs https://www.npmjs.com/package/cordova-plugin-dialogs
On a side note, there are better ways to manipulate DOM and CSS if that's your goal.
Upvotes: 1