0

In my page,I have to read the weather information from a third part site,then show the weather in the div if the user has the connection to the internet. If not,I will show some local content instead. So I have to check if the user have the connection.

Some people said that if the user can see my page,they must be have the connection. However it is not true all the time,since our application may run in the local network,so they can get the resource in our site but not the resource in the internet.

I have thougth use this manner:add the following code in the head of the page:

<head>
    <script src="http://www.google.com/xxx/jquery.min.xx.js" type="text/javascript"></script>
    <script type="text/javascript">
      window.onload=function(){
        if($){
          //the jquery is loaded,the client muse have the connection to the internet.
        } else
          //show local content
      }

    </script>
</head>

However,in our page we have used the prototype ,if the jquery is loaded successfully,it may cause conflict.

Any idea?

NOTE: we do not need the jquery in our page,here I load the jquery just for test the connection,if possible, I can load anyother lib .

Now,I find an idea:

if google host a js file internet.js,just one line:

var xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="xx";

Then I requset this file ,to test if the "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" variale is defined. :)))

Sam
  • 7,252
  • 16
  • 46
  • 65
hguser
  • 35,079
  • 54
  • 159
  • 293
  • You could use http://api.jquery.com/jQuery.noConflict/ to avoid the conflict. – Niklas Jun 04 '11 at 14:27
  • You can load prototype from google's site just as well as jQuery: http://code.google.com/apis/libraries/devguide.html#prototype . – Gijs Jun 04 '11 at 14:28
  • But if the user doesn't have internet connection, how would the user ever open and load your webpage and even then also with that jQuery script on a CDN? :) I think you need to reframe the question "How to check if an external domain/service is reachable?" or something. – BalusC Jun 04 '11 at 14:31
  • @Niklas:the jquery.noConflict does not work all the time. – hguser Jun 04 '11 at 14:33
  • @Gijs: how about if the user does not have the connection,so he can not load the prototype – hguser Jun 04 '11 at 14:34
  • 1
    @hguser Out of curiousity, when does it not work? – Niklas Jun 04 '11 at 14:35
  • _the prototype_ == prototype.js, right? – TweeZz Jun 04 '11 at 15:04

4 Answers4

1

Each JS library I know (Mootools/ JQuery) when they do AJAX, they also manage different return codes. One of those codes is "server not reachable" (find the relevant numeric code). You can check the return code and act upon it. Learn the specific API you use to see how to get those codes.

Since you say that some times the user won't have an outside connection, I would try to include (JSONP) a script from the queried site, which in turn loads the actual content, while this script is not active, show the local content. I would do this entire process using timed loop (setInterval)

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
  • But it is not allowed to do the ajax to a cross domain unless use the proxy manner. – hguser Jun 04 '11 at 14:32
  • @hguser: you've just already given the answer yourself (note: crossdomain requests are allowed if the server explicitly allows it by right headers and/or JSONP). – BalusC Jun 04 '11 at 14:34
  • @BalusC: In my post,I have said that our application may run in a local network without the internate connection. – hguser Jun 04 '11 at 14:36
  • @hguser: so you're working in Google.com? You're namely downloading jQuery from there. Again, a proxy should work perfectly fine from in intranet. – BalusC Jun 04 '11 at 14:39
  • @BalusC:Absolutely not. :) I load the jquery at google just for testing if the internet is avaiable. – hguser Jun 04 '11 at 14:44
1

What about checking if jquery is loaded? This might help.. http://jquery-howto.blogspot.com/2009/03/check-if-jqueryjs-is-loaded.html

NOTE: Here we are checking for jQuery function being defined or not. This is a safe way to check for jQuery library being loaded. In case you are not using any other javascript libraries like prototype.js or mootools.js, then you can also check for $ instead of jQuery.

Does it help when you change your check from if ($) to if (jquery)?

TweeZz
  • 4,779
  • 5
  • 39
  • 53
1

For your usecase, the best way would be to check the error/return code for the weather content you're loading. If it somehow errors or doesn't load, display your local content.

If you still want to insist on checking connectivity by loading a JS library, you can do something akin to the following:

<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
<script>
    if (typeof window.$ !== 'function') {
         window.noInternet = true;
         document.write('<script src="http://mylocalserver/prototype.js"></scri' + 'pt>');
    }
</script>

Possibly replacing 1.7.0.0 with your desired version of prototype.js

Gijs
  • 5,201
  • 1
  • 27
  • 42
0
if(navigator.online){
 //enter code here
}
Nick Vu
  • 14,512
  • 4
  • 21
  • 31