Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check internetconnection

    Could i check (in javascript) if i have an valid internet connection? I can´t use navigator.onLine because the site is an combination of online/offline site

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by janne67
    the site is a combination of online/offline site
    how so?

    surely you can still use navigator.online? all you need to do is make sure you handle your condition properly.
    *keep it simple (TM)

  • #3
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    you can try to load an image that's located on the server. Image objects have an onerror event handler that fires when they don't load for any reason. in this case, you could do something like the following

    <script>
    var img = new Image()
    img.onerror = function() { alert("Could not connect to the server; please check your network connection"); }
    img.src = "http://www.server.com/images/yourimage.gif";
    </script>


    it's important to note that you have to define the event handler before you assign the image object it's source.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #4
    New Coder
    Join Date
    Nov 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks i think this solves my problem

  • #5
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    This solves my problem too, I have posted the same question recently, but how do you code it so once you push OK on the alert, it takes you to a new page?
    -DR.Wong

    Wheres the food at?

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    <script>
    var img = new Image()
    img.onerror = function() { alert("Could not connect to the server; please check your network connection"); location.href='http://www.google.com'; }
    img.src = "http://www.server.com/images/yourimage.gif";
    </script>

    ?

  • #7
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Smile

    That works great, ONE LAST question for utter perfection of this code. If the image loads successfully, can you create an onload function that takes you to another page?
    -DR.Wong

    Wheres the food at?

  • #8
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Thumbs up

    Never mind, I thought it was more complicated,
    this is what I did:

    Code:
    <script>
    var img = new Image()
    onload=window.location="mypage.htm";
    img.onerror = function() { alert("Could not connect to the server; please check your network connection"); location.href='http://www.google.com'; }
    img.src = "http://www.url.com/img/pic.gif ";
    </script>
    Thanks for all the help
    -DR.Wong

    Wheres the food at?


  •  

    Posting Permissions

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