Forum Moderators: open
I could do it in the same page but I want the page to continue loading after the error has occurred.
Thanks.
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.
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);
}
?>
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?
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).
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.