2

I am using an iframe to embed a Google map in my page (intranet). If for whatever reason the user is not connected to the internet though the page should still load, just without the map.

I was thinking of using this:

    $.ajax({
        type: "GET",
        url: "http://www.google.com",
        timeout: "5000",
        error: function(msg) {
            $("#map").hide();
        }
    });

Two questions:

1) Currently it always fails even when I am online.

2) Is there a better page to ping instead of google.com? Maybe there is a lightweight Google API that would be better?

What am I doing wrong?

Thanks

b85411
  • 9,420
  • 15
  • 65
  • 119

2 Answers2

1

The problem is you are using $.ajax call to get data from another domain (and this fails for security reasons).

This is a way to do it. Right before your close body tag:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>

Complete answer: Check if Internet Connection Exists with Javascript?

Community
  • 1
  • 1
dminones
  • 2,236
  • 20
  • 21
0

An alternative is also setting up a server side proxy and using ajax to retrieve the results from the local server-side script. This would open up your options regarding how you would like to qualify your connectivity. If you chose to use any terminal commands or anything like that such as a ping command I highly recommend securing your server-side script (session validation, request rate limiting, etc.) as the address will be directly exposed in your client-side script.

eyegropram
  • 672
  • 4
  • 11
  • Maybe ping was the wrong choice of word. I just meant that I am looking for a simple way in jQuery to see if google is accessible - if it's not then I'll assume the maps aren't either and not display it. – b85411 Jan 29 '15 at 04:23
  • Using a proxy script would probably be the best method since ajax is confined by cross-origin restrictions. One way to bypass this limitation would be using a server-side proxy script. Cross-Origin Resource Sharing (CORS) mechanism is another option for domains you control – eyegropram Jan 29 '15 at 04:30