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 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Javascript not working as desired

    Greetings to Everyone
    Professionally I'm not a programmer. So i have no tough idea about variables,loops,functions etcetra, So can anybody help assemble this piece of Javascript to work properly.
    In both cases the script show either "OK" or "Down"
    i can't make it to work.
    I hope a programming guru will help me to fix this javascript code.
    Code:
    <html>
    <head>
    <title>My Intranet Page</title>
    </head>
    <body>
    <script>
    function doNoConnectFunction() {
    document.getElementById("intstatus").innerHTML = "Internet is Down";
    document.getElementById("wsstatus").innerHTML="Internal Web Server is Down";
    document.getElementById("fntcolor").style.color="#0000FF";
    }
    
    function doConnectFunction() {
    document.getElementById("intstatus").innerHTML = "Internet is Ok";
    document.getElementById("wsstatus").innerHTML="Internal Web Server is Ok";
    document.getElementById("fntcolor").style.color="#FF0000";
    } 
    
    var i = new Image();
    i.onload = doConnectFunction;
    i.onerror = doNoConnectFunction;
    i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + escape(Date());
    
    var j = new Image();
    j.onload = doConnectFunction;
    j.onerror = doNoConnectFunction;
    j.src = 'http://192.168.0.2/imgages/err.jpg' + escape(Date()); 
    
    </script>
    
    <font face="Arial">
    
    <b><font size="4">Works only on Javascript Enabled Browsers</font></b>
    <br>
    <br>
    <table border=1 cellpadding=2>
    <tr>
           <td align=right valign=top>Internet Status</td>
    	   <td><span id="fntcolor"><span id="intstatus"></span><font></td>
    </tr>
    <tr>
          <td align=right valign=top>Web Server Status</td>
           <td><span id="fntcolor"><span id="wsstatus"></span><font></td>
    </tr>
    </table>
    </body>
    </html>
    Thankyou for taking time to analyze the script
    layman
    Attached Files Attached Files

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    I see one error:
    Code:
    j.src = 'http://192.168.0.2/imgages/err.jpg' + escape(Date());
    That will produce an image named, for example, "err.jpgTuesday December 15..."

    You need a question mark there.

    I also suspect that "imgages" is spelled wrong.

    Code:
    j.src = 'http://192.168.0.2/images/err.jpg?' + escape(Date());
    I assume the purpose of this is to do "cache busting" so that the browser won't use the image it has stored in its cache??

    In that case, you'd be better off doing:
    Code:
    j.src = 'http://192.168.0.2/images/err.jpg?' + (new Date()).getTime();
    But you know, as that code is written, an error from *either* image will say that both the internet and intranet are down.

    Oh, wait...is that what you were asking about??? The problem you need solved??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Try this:
    Code:
    <html>
    <head>
    <title>My Intranet Page</title>
    <script type="script/javascript">
    function showConnection(which,okay) 
    {
        var cell = document.getElementById(which + "status");
        cell.innerHTML = "Internet is " + ( okay ? "Ok" : "Down");
        cell.style.color = ( okay ? "#00FF00" : "#FF0000" );
    }
    
    var i = new Image();
    i.onload = function( ) { showConnection("int",true); }
    i.onerror = function( ) { showConnection("int",false); }
    i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + (new Date()).getTime();
    
    var j = new Image();
    j.onload = function( ) { showConnection("ws",true); }
    j.onerror = function( ) { showConnection("ws",false); }
    j.src = 'http://192.168.0.2/images/err.jpg?d=' + (new Date()).getTime();
    </script>
    <style type="text/css">
    * { font-family: Arial, sans-serif; }
    td { font-size: medium; }
    span { font-size: large; font-weight: bold; }
    </style>
    </head>
    <body>
    <span>Works only on Javascript Enabled Browsers</span>
    <br>
    <br>
    <table border="1" cellpadding="2">
    <tr>
        <td style="text-align: right;">Internet Status</td>
        <td id="intstatus">waiting...</td>
    </tr>
    <tr>
        <td style="text-align: right;">Web Server Status</td>
        <td id="wsstatus">waiting...</td>
    </tr>
    </table>
    </body>
    </html>
    Last edited by Old Pedant; 12-15-2009 at 09:17 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    The script didn't worked

    hi Oldpedant
    the script didn't worked i thought that replacing "intstatus" and "wsstatus" with "int" and "ws" would work but didn't work either it just displays "waiting...."
    Code:
    <html>
    <head>
    <title>My Intranet Page</title>
    <script type="script/javascript">
    function showConnection(which,okay) 
    {
        var cell = document.getElementById(which + "status");
        cell.innerHTML = "Internet is " + ( okay ? "Ok" : "Down");
        cell.style.color = ( okay ? "#00FF00" : "#FF0000" );
    }
    
    var i = new Image();
    i.onload = function( ) { showConnection("int",true); }
    i.onerror = function( ) { showConnection("int",false); }
    i.src = 'http://www.aapress.com.au/img/71f.gif?d=' + (new Date()).getTime();
    
    var j = new Image();
    j.onload = function( ) { showConnection("ws",true); }
    j.onerror = function( ) { showConnection("ws",false); }
    j.src = 'http://192.168.0.2/images/err.jpg?d=' + (new Date()).getTime();
    </script>
    <style type="text/css">
    * { font-family: Arial, sans-serif; }
    td { font-size: medium; }
    span { font-size: large; font-weight: bold; }
    </style>
    </head>
    <body>
    <span>Works only on Javascript Enabled Browsers</span>
    <br>
    <br>
    <table border="1" cellpadding="2">
    <tr>
        <td style="text-align: right;">Internet Status</td>
        <td id="int">waiting...</td>
    </tr>
    <tr>
        <td style="text-align: right;">Web Server Status</td>
        <td id="ws">waiting...</td>
    </tr>
    </table>
    </body>
    </html>
    i think the script is very near to completion but cant' find out where it's going wrong.
    thanks

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Tested and working:
    Code:
    <html>
    <head>
    <title>My Intranet Page</title>
    <script type="text/javascript">
    function initTests()
    {
        var i = document.getElementById("testimage1");
        i.src = "http://www.artsofsnohomish.org/images/Santa2009Top.jpg?foo=" + (new Date()).getTime();
        var j = document.getElementById("testimage2");
        j.src = "http://localhost/nosuchthing.jpg?foo=" + (new Date()).getTime();
    }
    function showConnection(which,okay) 
    {
        var cell = document.getElementById(which + "status");
        cell.innerHTML = "Connection is " + ( okay ? "Ok" : "Down");
        cell.style.color = ( okay ? "#00FF00" : "#FF0000" );
    }
    </script>
    <style type="text/css">
    * { font-family: Arial, sans-serif; }
    td { font-size: medium; }
    span { font-size: large; font-weight: bold; }
    </style>
    </head>
    <body onload="initTests()">
    <span>Works only on Javascript Enabled Browsers</span>
    <br>
    <br>
    <table border="1" cellpadding="2">
    <tr>
        <td style="text-align: right;">Internet Status</td>
        <td id="intstatus">waiting...</td>
    </tr>
    <tr>
        <td style="text-align: right;">Web Server Status</td>
        <td id="wsstatus">waiting...</td>
    </tr>
    </table>
    <div style="visibility: hidden;">
    <img id="testimage1" alt="" style="width: 10px; height: 10px;"
         onload="showConnection('int',true);" onerror="showConnection('int',false);" />
    <img id="testimage2" alt="" style="width: 10px; height: 10px;"
         onload="showConnection('ws',true);" onerror="showConnection('ws',false);" />
    </div>
    </body>
    </html>
    MSIE, at least, would *not* trigger onload/onerror from an image that existed only as a JS object. So I made the "real" to the browser and they worked.

    That image from that Australian site no longer exists, by the by, so it would always give an error. You must use an image that truly exists, out on the internet. I'd suggest some google image or similar.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    layman (12-17-2009)

  • #6
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up Solved:

    Dear Old Pedant
    Thankyou verymuch for your effort to make this piece of script work properly.
    It worked like a charm on IE8 and Firefox 3.5
    It took me 4 days to come near around that code after massive "google".
    and you did it in ......... by the way, how long did it took you to make this script work. 1/2 hour ? 15 minutes?
    Code:
    anyways... you are "Senior,Fluent & Hard Coder" 
    always keep up your helping hands.
    Thanks Once again && Merry Christmas
    layman

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Well, once I gave up and decided to actually test it before posting (I didn't test at all first time), about 10 minutes. Maybe 15. About as fast as I could type, basically. <grin/>

    First thing I did was a dirt simple test similar to this:
    Code:
    <script>
    var i = new Image();
    i.onload = new function() { alert('okay'); }
    i.src = "...known good image ...";
    </script>
    When that didn't work the way I wanted to, I dumped it and went to the actual <img> tags instead.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    15Minutes is Excellent.
    If there's Programming Race, i suggest you to participate

    by the way does such game exist ? give all Pariticipants a problem and the winner will be the one who codes the fastest.
    The text below is not important.
    well during playing with the script i thougt that
    google is more reliable than www.aapress.com.au so i replaced this
    "http://www.aapress.com.au/img/71f.gif?d="
    with this
    Code:
    http://www.google.com/images/yellow_warning.gif
    and the script said internet is down but http://www.google.com/images/yellow_warning.gif does exist
    so imho is it due to google's security thing

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by layman View Post
    The text below is not important.
    well during playing with the script i thougt that
    google is more reliable than www.aapress.com.au so i replaced this

    with this
    Code:
    http://www.google.com/images/yellow_warning.gif
    and the script said internet is down but http://www.google.com/images/yellow_warning.gif does exist
    so imho is it due to google's security thing
    http://www.google.com/images/yellow_warning.gif must be in quotes.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Layman is correct.

    Try this URL in your browser:
    http://www.google.com/images/yellow_warning.gif?foo=1
    Code:
    http://www.google.com/images/yellow_warning.gif?foo=1
    Google gives you a 404 error (file not found).

    Whereas if you omit the "?foo=1" it works fine.

    So Google's servers do *NOT* ignore the extra part of the URL. I'm sure that Google's servers are highly customized for Google's uses, so this is not surprising.

    Okay, so look around and find another stable image source that works.

    You *CAN* just test this in your browser, without needing that JS code. Just append "?foo=1" to the end of the image URL and see if it still works.

    Here is one that works:
    Code:
    http://www.codingforums.com/img/logo.gif?foo=123
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    layman (12-18-2009)

  • #11
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Thanks Philip as mentioned by you. It was already inside quotes. Sorry i forgot to mention quotes in my reply.
    Thanks

  • #12
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Layman is correct.

    Try this URL in your browser:
    http://www.google.com/images/yellow_warning.gif?foo=1
    Code:
    http://www.google.com/images/yellow_warning.gif?foo=1
    Google gives you a 404 error (file not found).

    Whereas if you omit the "?foo=1" it works fine.

    So Google's servers do *NOT* ignore the extra part of the URL. I'm sure that Google's servers are highly customized for Google's uses, so this is not surprising.

    Okay, so look around and find another stable image source that works.

    You *CAN* just test this in your browser, without needing that JS code. Just append "?foo=1" to the end of the image URL and see if it still works.

    Here is one that works:
    Code:
    http://www.codingforums.com/img/logo.gif?foo=123
    Thanks Old Pedant
    Now i got it.
    It's google ... Not the Script.


  •  

    Posting Permissions

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