SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS 'heyoffline' check for event and then redirect if online

    Hi there,

    I'm using the 'heyoffline' script (https://github.com/oskarkrawczyk/heyoffline.js) to determine whether users have an active internet connection, and if not, show a notification.

    What I want to do is onload, check if they are online, and if so, redirect to another page, but if offline, load as normal. I'm not sure how to code this.

    Basically, there are 2 events: onOnline and onOffline - but my javascript skills are so minimal, I cant work out how to check if one event is fired when the page is loaded.

    I want to accomplish this:

    If onOnline = true, then redirect, otherwise just load page as normal.

    Any help would be great.

    Thanks

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,858
    Mentioned
    206 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    That's an interesting script.
    I've just been having a look at the code and it seems to use navigator.onLine to detect if one is online or not.

    I'm not sure how to implement this with Heyonline, but you could write something like this in plain JavaScript:

    Code JavaScript:
    window.onload= function(){
      if (navigator.onLine){
        window.location = "http://www.sitepoint.com";
      }
      console.log("Offline");
    }

    Please be aware, that this doesn't seem to be reliable 100% of the time, as is discussed here: http://news.ycombinator.com/item?id=4818380

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastic, thanks Pullo!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •