1
votes

This is a page where I am attempting to load a backup from the file system if the device cannot connect

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/about.css"/>
</head>

<body>
    <ons-screen>
        <ons-navigator title = "About">
            <ons-page id="page" font-align="center">

            </ons-page>
        </ons-navigator>
    </ons-screen>
</body>
<script type="text/javascript">
    var page = document.getElementById("page");

        if(navigator.connection.type != Connection.NONE){
            page.innerHTML='<iframe src="http://www.website.com/about/" style="width:100%; height:100%;"></iframe>';
        }else{
            page.innerHTML='<iframe src="aboutBackup.html" style="width:100%; height:100%;"></iframe>';
        }
</script>
</html>

I installed the plugin using the github instructions (https://github.com/apache/cordova-plugin-network-information/blob/master/doc/index.md)

I can't figure out why, but I get Uncaught TypeError: Cannot read property 'type' of undefined on line 22 where I check the connection type.

1

1 Answers

0
votes

You need to add cordova.js in your file.

<script type="text/javascript" src="cordova.js"></script>

Then listen to device ready function and shot for the GOAL

<script>
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady()
{

    if(navigator.connection.type=='none')
    {
         alert("Wait I'm offline.");
    }
    else
    {
          page.innerHTML='<iframe src="aboutBackup.html" style="width:100%; height:100%;"></iframe>';
    }
}
</script>

Anything you do have to be after device ready function is fired. And without cordova.js cordova plugins won't work.