title: Phonegap - Delete contacts makes my app no work more
body: <p>I developed a simple phonegap app to manage contacts.</p>
  
  <p>This part list all the contacts and with a button, when this is clicked, delete the contact. Everything works fine but when I delete one contact succesfully, I want to reload my web to see the new list without the contacts deleted but my app don't load correctly.</p>
  
  <p>I have no idea what the problem could be...</p>
  
  <p>The code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;Contact Example&lt;/title&gt;
  
      &lt;script type="text/javascript" charset="utf-8" src="cordova.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" charset="utf-8"&gt;
  
      // Wait for device API libraries to load
      //
      document.addEventListener("deviceready", onDeviceReady, false);
  
      // device APIs are available
      //
      function onDeviceReady() {
          // specify contact search criteria
          var options = new ContactFindOptions();
          options.filter = "";      // empty search string returns all contacts
          options.multiple = true;  // return multiple results
          filter = ["displayName","phoneNumbers"]; // return contact.displayName field    displayName
  
          // find contacts
          navigator.contacts.find(filter, onSuccess, onError, options);
      }
  
      // onSuccess: Get a snapshot of the current contacts
      //
      function onSuccess(contacts) {
  
        for (i=0;i&lt;contacts.length;i++) {
          //window.alert("ID = " + contacts[i].id);
          document.write(contacts[i].displayName);
          tel = contacts[i].phoneNumbers;
          if (tel== null){
  document.write("Sense telefon");
          }else{
                  for (j=0;j&lt;contacts[i].phoneNumbers.length;j++) {
              if (contacts[i].phoneNumbers != null &amp;&amp;
  contacts[i].phoneNumbers != 'undefined') {
                  document.write(" "+contacts[i].phoneNumbers[j].value);
              }
          }
          }
          //document.write('&lt;button id='+i+' onClick="reply_click(this.id,contacts);"&gt;Borrar&lt;/button&gt;');
          var button = document.createElement("button"); 
          button.id = i; 
          button.onclick = function() { reply_click(this.id, contacts); }; // append button to DOM
          document.body.appendChild(button);
          document.write(contacts[i].id);
          document.write("&lt;br/&gt;");
      }
      };
  
      function reply_click(clicked_id,contacts)
      {
  
          window.alert(clicked_id);
          window.alert(contacts);
           for (i=0;i&lt;contacts.length;i++) {
           if(i==clicked_id){
           try{
              persona = contacts[i];
              persona.remove(onRemoveSuccess,onRemoveError);
              window.alert("Contacte Borrat");
              setTimeout("location.href='buscacont.html'", 5000);
              }catch(err){
              window.alert(err);
              }
              }
           }
  
      }
      function onError(contactError) {
          window.alert('onError!');
      }
      function onRemoveSuccess(contacts) {
           window.alert("Removal Success");
      }
  
      // onRemoveError: Failed to get the contacts
      //
      function onRemoveError(contactError) {
           window.alert("Error = " + contactError.code);
      }
  
  
      &lt;/script&gt;
  
    &lt;/head&gt;
    &lt;body&gt;
      &lt;h1&gt;Example&lt;/h1&gt;
      &lt;p&gt;Find Contacts&lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The logs didn't say anything usefull.</p>
  
  <p><strong>EDIT: ADDITTIONAL INFO</strong></p>
  
  <p>My other page has this code, it have a different structure:</p>
  
  <pre><code>var app = {
      // Application Constructor
      initialize: function() {
          this.bindEvents();
      },
      // Bind Event Listeners
      //
      // Bind any events that are required on startup. Common events are:
      // 'load', 'deviceready', 'offline', and 'online'.
      bindEvents: function() {
          document.addEventListener('deviceready', this.onDeviceReady, false);
      },
      // deviceready Event Handler
      //
      // The scope of 'this' is the event. In order to call the 'receivedEvent'
      // function, we must explicity call 'app.receivedEvent(...);'
      onDeviceReady: function() {
  
      //function prova(){
  
      //}
      /*
          app.receivedEvent('deviceready');
          var myContact = navigator.contacts.create({"displayName": "Test User"});
          myContact.note = "This contact has a note.";
          console.log("The contact, " + myContact.displayName + ", note: " + myContact.note);
          app.receivedEvent('contactcreated');
          myContact.save(app.onContactSaveSuccess,app.onContactSaveError);
          */
      },
      // 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);
      },
      onContactSaveSuccess: function(contact) {
          window.alert("Save Success");
      },
      onContactSaveError: function(contact) {
          window.alert("Save Failed");
      },
  
  };
  
  function doFunction(){
  window.alert("Saaaaaaaaaaaaaaaaaaave Success");
  var myContact = navigator.contacts.create({"displayName": "Test LELO"});
          myContact.note = "This contact has a note.";
          console.log("The contact, " + myContact.displayName + ", note: " + myContact.note);
          app.receivedEvent('contactcreated');
          myContact.save(app.onContactSaveSuccess,app.onContactSaveError);
          window.alert("Saaaaaaaaaaaaaaaaaaave Success");
  }
  
  function creaContacte()
  {
  var x = document.getElementById("frm1");
  var txt = "";
    nom = x.elements[0].value;
    num= x.elements[1].value;
    email= x.elements[2].value;
  
          var myContact = navigator.contacts.create({"displayName": nom});
          myContact.note = "Contacte creat amb MarcGarciaContactManager";
          myContact.name = nom;
  
          var phoneNumbers = [];
          phoneNumbers[0] = new ContactField('General', num, true);
          myContact.phoneNumbers = phoneNumbers;
  
  
          var emails = [];
          emails[0] = new ContactField('General', email, true);
          myContact.emails = emails;
  
          console.log("The contact, " + myContact.displayName + ", note: " + myContact.note);
  
          myContact.save(app.onContactSaveSuccess,app.onContactSaveError);
          window.alert("Contact creat");
  
  }
  function buscacon()
  {
   document.addEventListener("deviceready", onDeviceReady, false);
  
      // PhoneGap is ready
      //
      function onDeviceReady() {
          // find all contacts with 'Bob' in any name field
          var options = new ContactFindOptions();
          options.filter="pepepe"; 
          var fields = ["displayName", "name"];
          navigator.contacts.find(fields, onSuccess, onError, options);
      }
  
      // onSuccess: Get a snapshot of the current contacts
      //
      function onSuccess(contacts) {
          for (var i=0; i&lt;contacts.length; i++) {
              window.alert("Display Name = " + contacts[i].displayName);
          }
      }
  
      // onError: Failed to get the contacts
      //
      function onError(contactError) {
          alert('onError!');
      }
  
  
  }
  </code></pre>
  
  <p>It's a simple code to add contacts, the html part i didn't add it because is a simple form and a javascript call function...</p>
  
  <p>The strange is that when I run the app..... If first create a contact using this page, after I can check all the time without problems the other page (the page to see al the contacts), but if I check first the page to see al the contacts, this crashes the second time I visit it (not load properly the deviceready or whatever..)</p>