Forum Moderators: open

Message Too Old, No Replies

thoughts on javascript error trapping

         

jackvull

3:33 pm on Oct 10, 2005 (gmt 0)

10+ Year Member



Hi
I have some JavaScript on my site that traps any client sode errors and opens a new pop up window displaying the error and emailing me the details (PHP script).
Just wondering if there would be an alternative way to go about this considering that many browsers block pop-ups automatically now and if they are blocked then I won't receive any alerts about JavaScript errors.

I could do it in the same page but I want the page to continue loading after the error has occurred.

Thanks.

Bernard Marx

8:26 pm on Oct 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use an iframe.

jackvull

7:57 pm on Oct 13, 2005 (gmt 0)

10+ Year Member



I've played around with this but can't seem to get the javascript to open a PHP page inside the iframe.

What I want to do is trap the error (I use a handleError function to do this,w hich captures the error message and the line it occurred on). I then want to pass this in the querystring to the iframe...something like: window.open( javascriptError.php?msg=object+not+found&ln=127)

Can I do this with a target attribute or should I use the getElementById('iframe') solution?

Thanks.

Bernard Marx

9:30 pm on Oct 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had a quick go at it. The PHP is basic (just my level).

3 Files: {test.htm, error.php, errorLog.txt }


---- errorLog.txt ------------------------------------

( empty! )

---- test.htm ----------------------------------------

<html><head><title>TEST</title></head>
<script type="text/javascript">
var errors =
[
'msg=something+wrong&ln=34',
'msg=bad+thingy&ln=24',
'msg=awful+code&ln=1',
'msg=cant+do+that&ln=99'
]

var iE = 0;

function dummyError()
{
var edisplay = document.getElementById('edisplay');
if(iE<errors.length)
{
var error = edisplay.value = errors[iE++];
document.frames.eframe.location.replace('error.php?'+error);
}
else
edisplay.value = 'no more errors';
}
</script>
<body>
<button onclick="dummyError()">Create Error</button>
<input id="edisplay" type="text" value="" size="40"><br>
<iframe id="eframe" name="eframe" width="300" height="100"
style="display:none;"></iframe>

</body>
</html>

---- error.php --------------------------------------------

<?php
if(isset($_GET['msg']))
{
$str = 'msg: '.$_GET['msg'].';ln: ' .$_GET['ln'].";\n";
}

$file = fopen('errorLog.txt','a');
if($file)
{
fputs($file,$str);
fclose($file);
}
?>

jackvull

11:04 am on Oct 14, 2005 (gmt 0)

10+ Year Member



Thanks - that works very well - much appreciated!

Just one last question...I have noticed that if an error occurs before the rest of the page loads then the JavaScript won't actually pick up the error. This seems to be because the iframe has not yet been 'set', so it throws a 'not an object' error.

I'm having difficulty sorting this one out as obviously the error handling JavaScript has to be before all other JavaScript but it has to come after the iframe to recognise it.

Could I just put all the JavaScript includes, etc. at the bottom of the PHP scripts, etc. or does proper coding mean it has to be in the within the <head> tags?

Bernard Marx

11:49 am on Oct 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



proper coding - That depends on who you ask.

Purists, who are really into that "separation of behaviour from content" thing, are against using script tags within the <body> element - there's no validation problem with it, as such.

Personally, I think it can be a good workaround for some problems. I think browsers aren't really ready for the purest application of these high ideals, so we should act accordingly. Anyway, it's just a few script tags at the bottom of the doc. Who's it gonna hurt?

If you're at risk of being told off for having script elements in the body, then you could make sure that no scripts run until the page has loaded (a big topic in itself).

There are couple of possible alternative approaches:

1) Use an XMLHTTPRequest

Added complication, and backward compatibility probably worse than Iframes. Possible ActiveX problems in IE.

2) Use a dummy <script> or <link> element.

Since all we're doing is requesting a url, perhaps it could be done with an element that can appear in the HTML before your other scripts. Either of these elements might do it. Have a go (I might try it later myself).

Bernard Marx

12:10 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cut that waffle. It seems to work with an image, created with the Image constructor (in fact, I have seen this done before). Simply..

1) Get rid of that iframe.

2) Replace this line in the script:

document.frames.eframe.location.replace('error.php?'+error);

with this:

(new Image).src = 'error.php?'+error;

Notes:

i) This creates image objects that are immediately orphaned. They still seem to make their request. If you are paranoid about this, you could create a global array and push the image into that each time, just so a reference to it is preserved.

ii) I noticed that the script block in my code was itself inside the body tag. Purely accidental that one.