1

i have very interesting problem in my Phonegap App for WP8.1. When i use function

document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady(){
  **obj.alertFunction();**
}

var obj = {
   alertFunction: function(){alert('HelloWorld');}
}    

nothing is happen, but when i use only alertFunction(); where alertFunction is

function alertFunction(){
  alert('Hello');
}

all is OK. On Android is all OK and on Ripple Simulator too. only on Windows Phone device i have this problem. THX for answer :)

bajky
  • 332
  • 6
  • 17

1 Answers1

0

It could be because you are using a function expression in the first case but a function statement in the second case for the alert. Function expressions are executed in a procedural fashion. Try changing the execution order:

function onDeviceReady(){
  **obj.alertFunction();**
}

var obj = {
   alertFunction: function(){alert('HelloWorld');}
}   

document.addEventListener("deviceready",onDeviceReady,false);

Here's a snippet illustrating the difference:

  1. OnDeviceReady() executed on top:

onDeviceReady();

function onDeviceReady(){
  obj.alertFunction();
}

var obj = {
   alertFunction: function(){alert('HelloWorld');}
}
Nothing happens
  1. OnDeviceReady() executed at the bottom:

function onDeviceReady(){
  obj.alertFunction();
}

var obj = {
   alertFunction: function(){alert('HelloWorld');}
}    

onDeviceReady();
Alert is triggered

If changing the execution doesn't work, you might need to rewrite your code into a function statement.

More reading:

Community
  • 1
  • 1
James Wong
  • 4,529
  • 4
  • 48
  • 65