1 Replies - 3739 Views - Last Post: 02 November 2012 - 02:33 AM

#1 Cpp_noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 06-July 09

jQuery Mobile - Detecting Internet Connection

Posted 13 October 2012 - 07:16 AM

i want to prevent the user from accessing a page in my mobile app unless the have a connection
i use this code but it doesnt seem to work

$(document).ready(function()
{
 $('.MainLink').click(function() 
    {
        if (!window.navigator.onLine) 
        {
            return false;
        }         
     });
});


Is This A Good Question/Topic? 0
  • +

Replies To: jQuery Mobile - Detecting Internet Connection

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: jQuery Mobile - Detecting Internet Connection

Posted 02 November 2012 - 02:33 AM

Hello Cpp_noob,

Have you checked that the window.navigator.onLine is actually returning the correct value? A simple alert of the value should indicate that for you.

Next, be aware not 'all' browsers support this DOM property just yet, although most do (see browser compatibility here).

Lastly, and what i suspect might be your issue, just before you return false try preventing the default events from firing by using e.preventDefault(); the line before. You will need to declare e as a parameter for the click function.

$(document).ready(function()
{
 $('.MainLink').click(function(e) 
    {
        if (!window.navigator.onLine) 
        {
			e.preventDefault();
            return false;
        }         
     });
});


Reference: http://api.jquery.co...preventDefault/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1