Working with Jquery When There Is No Internet Connection

Working with jQuery when there is no Internet connection

Yes, it's possible. You've mentioned that you downloaded the file - that's a good first step, but you also have to change all the href and src references.

For example,

<link rel="stylesheet" href="http://jquery.com/jquery-wp-content/themes/jquery/css/base.css?v=1">

should become

<link rel="stylesheet" href="base.css">

Also remember to get the offline version of the jQuery JS library, too:

Download jquery.js, put it in your root folder & reference it:

<script src="jquery-1.9.1.min.js"></script>

And if you want it in a subdirectory:

<script src="[subdirectory-name]/jquery-1.9.1.min.js"></script>

Remember that both files need to be offline and in your working local directory. This means that you cannot remove the stylesheet nor the jQuery JS library. Keep both of them, in the local formats I've mentioned above.

Also, putting the <script> tag in the <head> is bad practice; move it just before the </body> tag instead.


Your code should now look (in short, note that I haven't put much) like:

...
<html class="no-js" lang="en-US">
<head>
...
<link rel="stylesheet" href="base.css">
...
</head>
<body>
...
<script src="jquery.min.js"></script>
</body>
</html>

Again, make sure that base.css and jquery.min.js are the exact file names and are in the same folder as this .html file

Check if no Internet Connection - Popup - Windows 10 App(Javascript)

you can use the NetworkConnectivityLevel, NetworkInformation.getInternetConnectionProfile and getNetworkConnectivityLevel to do this, and show the information with a MessageDialog in the default.js like this:

var connections = Windows.Networking.Connectivity.NetworkInformation.getInternetConnectionProfile();
if (connections != null) {
var networkConnectivityLevel = connections.getNetworkConnectivityLevel();
if (networkConnectivityLevel == Windows.Networking.Connectivity.NetworkConnectivityLevel.internetAccess) {
var msg = new Windows.UI.Popups.MessageDialog("Internet access OK.");
} else if (networkConnectivityLevel == Windows.Networking.Connectivity.NetworkConnectivityLevel.constrainedInternetAccess) {
var msg = new Windows.UI.Popups.MessageDialog("Limited internet access.");
} else if (networkConnectivityLevel == Windows.Networking.Connectivity.NetworkConnectivityLevel.localAccess) {
var msg = new Windows.UI.Popups.MessageDialog("Local network access only.");
} else if (networkConnectivityLevel == Windows.Networking.Connectivity.NetworkConnectivityLevel.none) {
var msg = new Windows.UI.Popups.MessageDialog("No internet access.");
}
msg.showAsync();
} else {
var msg = new Windows.UI.Popups.MessageDialog("No internet access.");
msg.showAsync();
}

Check Internet connectivity with jquery

$.get() returns a jqXHR object, which is promise compatible - therefore no need to create your own $.Deferred.

var check_connectivity = {
...
is_internet_connected: function() {
return $.get({
url: "/app/check_connectivity/",
dataType: 'text',
cache: false
});
},
...
};

Then :

check_connectivity.is_internet_connected().done(function() {
//The resource is accessible - you are **probably** online.
}).fail(function(jqXHR, textStatus, errorThrown) {
//Something went wrong. Test textStatus/errorThrown to find out what. You may be offline.
});

As you can see, it's not possible to be definitive about whether you are online or offline. All javascript/jQuery knows is whether a resource was successfully accessed or not.

In general, it is more useful to know whether a resource was successfully accessed (and that the response was cool) than to know about your online status per se. Every ajax call can (and should) have its own .done() and .fail() branches, allowing appropriate action to be taken whatever the outcome of the request.



Related Topics



Leave a reply



Submit