Search code examples
javascriptjquerycordovaonsen-ui

Run process before load page


i am using cordova 6 with onsen-ui,jquery and javascript. So i am trying to make a simple login site, but i need to get if gps is active. I want to know WHERE i must do that validation. Now I do the following on ons.ready() event.

var options = {maximumAge: 0, timeout: 3000, enableHighAccuracy:true};
navigator.geolocation.getCurrentPosition(onGPSCheckSuccess, onGPSCheckError, options);

function onGPSCheckSuccess()
{
    console.log("Encontro el GPS Activado");
}

function onGPSCheckError()
{
    console.log("Error al chequear el GPS");
    gpsDetect.switchToLocationSettings(onSwitchToLocationSettingsSuccess,     onSwitchToLocationSettingsError);
}

function onSwitchToLocationSettingsSuccess() {
}

function onSwitchToLocationSettingsError(e) {
  console.log("Error al activar el GPS");
  alert("Error onSwitchToLocationSettingsError: "+e);
}

So if there a way to do this BEFORE my main page es loaded?

Regards


Solution

  • I think the best way to do this, it with the following code:

    document.addEventListener("init", function(event){
        if(event.target.id=='yourHomePageID') {
          var options = {maximumAge: 0, timeout: 3000, enableHighAccuracy:true};
          navigator.geolocation.getCurrentPosition(onGPSCheckSuccess, onGPSCheckError, options);
        }
    },false);
    

    By using the init function for the homepage, this actually runs a bit before ons.ready() as discussed here by @fran-dios:

    Onsen 2.0 - Adding event listener to Ons-Switch with Javascript

    Hope this helps. It is what I am doing based on the aforementioned question and it solved my issue.