Catching reload error

D

Duncan McNiven

I am new to JavaScript. My script works in FireFox (2.0.0.4) but not in
IE (7.0.5730.11 and 6.0.2800.1106) . I have an HTML file that users
open over a LAN. There is no WWW access. The file includes a script
that can automatically reload the page. When the file is missing or
being updated the reload obviously fails. I want to swallow the error
and try again a little later. I don't want the user to see any error
message.

This code works in Firefox:

function wait(msecs)
{
var start = new Date().getTime();
var cur = start;
while (cur-start < msecs)
{
cur = new Date().getTime();
}
}

function doReload()
{
var Reloaded = false;
while (!Reloaded)
{
try
{
location.reload(true);
Reloaded = true;
}
catch(err)
{
wait(500);
}
}
}

wait is not my code. I found it at
http://snippets.dzone.com/posts/show/5828. Oh, and I do realise that I
need code to prevent the while loop running forever if the file never
becomes available. I stripped that out for testing/demo.

IE Says "Internet Explorer cannot display the webpage". I cannot find
any more detailed error message.

I added code to try and display the error details, like this:

catch(err)
{
ErrTxt="doReload error\n\n"
ErrTxt+="Error: " + err.description;
alert(ErrTxt);
wait(500);
}

Firefox describes the error as "undefined". IE Does not display the
alert.

I traced execution in IE's debugger, and found that even when the
location.reload(true); fails (because I deleted the file to be loaded),
IE continues to the next line (Reloaded = true;). I expected an
exception to be thrown so that execution would jump to the catch code,
but that doesn't happen. Why not?

Is my basic approach sensible? If it is, how do I get this working in
IE?

--
 
R

RoLo

I am new to JavaScript. My script works in FireFox (2.0.0.4) but not in
IE (7.0.5730.11 and 6.0.2800.1106) . I have an HTML file that users
open over a LAN. There is no WWW access. The file includes a script
that can automatically reload the page. When the file is missing or
being updated the reload obviously fails. I want to swallow the error
and try again a little later. I don't want the user to see any error
message.

This code works in Firefox:

      function wait(msecs)
      {
        var start = new Date().getTime();
        var cur = start;
        while (cur-start < msecs)
        {
          cur = new Date().getTime();
        }
      }

      function doReload()
      {
        var Reloaded = false;
        while (!Reloaded)
        {
          try
          {
            location.reload(true);
            Reloaded = true;
          }
          catch(err)
          {
            wait(500);
          }
        }
      }

wait is not my code. I found it athttp://snippets.dzone.com/posts/show/5828. Oh, and I do realise that I
need code to prevent the while loop running forever if the file never
becomes available. I stripped that out for testing/demo.

IE Says "Internet Explorer cannot display the webpage". I cannot find
any more detailed error message.

I added code to try and display the error details, like this:

      catch(err)
      {
        ErrTxt="doReload error\n\n"
        ErrTxt+="Error: " + err.description;
        alert(ErrTxt);
        wait(500);
      }

Firefox describes the error as "undefined". IE Does not display the
alert.

I traced execution in IE's debugger, and found that even when the
location.reload(true); fails (because I deleted the file to be loaded),
IE continues to the next line (Reloaded = true;). I expected an
exception to be thrown so that execution would jump to the catch code,
but that doesn't happen. Why not?

Is my basic approach sensible? If it is, how do I get this working in
IE?

--

reload would do a refresh of the same page, don't know what behavior
would occour if the reload success. My guess is that it will stop the
script.
Since its the same page, I think it would be better if you use
XMLHttpRequest since it gives you the status code of the HTTP server
response. If its NOT 200 would mean the page was deleted.

https://developer.mozilla.org/En/AJAX:Getting_Started
 
D

Duncan McNiven

RoLo said:
reload would do a refresh of the same page,

Good. That is what I want to do. The page contains status data that may
have changed since the page was loaded. I want to reload it to show the
latest data.

don't know what behavior would occour if the reload success.
My guess is that it will stop the script.

That is my guess (hope) also: the script should stop because the page
will be replaced with the new copy. The script on the new page will
then run. This is what happens in Firefox. (The script on the new
version of the page may or may not be the same as the script on the
previous version of the page).

Since its the same page, I think it would be better if you use
XMLHttpRequest

Thanks for the suggestion. XMLHttpRequest is something else new to me.
I did look at it, but dismissed it for 2 reasons.
The first reason is that it is for communication with server-side
scripts, but I don't have any server-side scripts and I don't have a
server as such. I just have an HTML file. So my reading is that
XMLHttpRequest is not a good match for this problem.
The second reason I rejected XMLHttpRequest is because of a code
example I found. This used XMLHttpRequest to get a status code. It then
used location.reload to refresh the page. This means I am back in the
same situation, if the file is deleted between the XMLHttpRequest and
the location.reload. My script would still fail, and I still need a
graceful way to handle that failure.

Is my logic/understanding wrong?

--
 
D

Dr J R Stockton

Sun said:
function wait(msecs)
{
var start = new Date().getTime();
var cur = start;
while (cur-start < msecs)
{
cur = new Date().getTime();
}
}

Using a wait loop on a multi-process OS is generally bad practice,
particularly in JavaScript; you should be able to use setTimeout to
invoke your re-tries.

That having been said, it is generally bad practice to write inefficient
loops. That one contains unnecessary arithmetic and assignment. Think
about
var final = +new Date() + msecs
while (new Date() < final) ; // remember semicolon.
// do ; while (new Date() < final) // alternative

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
R

RoLo

The first reason is that it is for communication with server-side
scripts, but I don't have any server-side scripts and I don't have a
server as such. I just have an HTML file. So my reading is that

XMLHttpRequest can load the static HTML if its from the same origin.
https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript
The second reason I rejected XMLHttpRequest is because of a code
example I found. This used XMLHttpRequest to get a status code. It then
used location.reload to refresh the page. This means I am back in the

You could use the same data received by the XMLHttpRequest and change
the old one... without reloading..

If you can control the data format, instead of creating a full html,
create just the part to substitude.
Then replace the old data with something like: document.getElementById
('data_box').innerHTML=newdata;

Using an example from: https://developer.mozilla.org/En/AJAX:Getting_Started

<script type="text/javascript" language="javascript">
function makeRequest(url) {
var httpRequest;

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType('text/xml');
// See note below about this line
}
}
else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject
("Microsoft.XMLHTTP");
}
catch (e) {}
}
}

if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function() { alertContents
(httpRequest); };
httpRequest.open('GET', url, true);
httpRequest.send('');

}

function alertContents(httpRequest) {

if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
alert(httpRequest.responseText);

} else {
alert('There was a problem with the request.');
}
}

}
</script>
<div id="data_box"></div>
<span
style="cursor: pointer; text-decoration: underline"
onclick="makeRequest('data.html')">
Update Data
</span>
 
R

RoLo

you'll need to change this function:

    function alertContents(httpRequest) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
document.getElementById
().innerHTML=httpRequest.responseText;
            } else {
                alert('There was a problem with the request.');
            }
        }

    }

Rolo
 
D

Duncan McNiven

RoLo said:
XMLHttpRequest can load the static HTML if its from the same origin.
https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript

OK, Good.

You could use the same data received by the XMLHttpRequest and change
the old one... without reloading..

OK. I do have full control over the data, so I can do whatever is
needed to get this working. It is disappointing (and surprising to me)
that IE cannot catch a failure in location.reload, but such is life I
guess.


Thanks a lot for the example code. I have tried it, and encountered a
couple of problems in the makeRequest routine. I can code around both
of them, but I'll note them here anyway. The test:

if (window.XMLHttpRequest) { // Mozilla, Safari, ...

returns true when I run in IE. I expected it to be false. I can make
other tests to identify the browser, but this is odd.

The other minor change is that I get an httpRequest.status of 0. I
found that is OK, in that I get 0 instead of 200 because I am dealing
with a file. If I test for 0 instead of 200 I can get the
httpRequest.responseText OK.

Anyway, thanks a lot for the help. I can now move forward from here.
I'll now take a look at implementing the replacement of the old data
with the new data.

--
 
D

Duncan McNiven

Dr said:
Using a wait loop on a multi-process OS is generally bad practice,
particularly in JavaScript; you should be able to use setTimeout to
invoke your re-tries.

Thanks. Point taken.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top