Closed Thread Icon

Topic awaiting preservation: Avoiding errors (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8163" title="Pages that link to Topic awaiting preservation: Avoiding errors (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Avoiding errors <span class="small">(Page 1 of 1)</span>\

 
viol
Maniac (V) Inmate

From: Charles River
Insane since: May 2002

posted posted 05-10-2002 22:49

In some part of the body of my html code, I have:

<script language="JavaScript" type="text/javascript">Wpic();</script>

Wpic() is a function that writes part of the code to show a picture.

On the header, I have:

<script language="JavaScript" type="text/JavaScript" src="popup.js"></script>

Let's suppose the visitor save the html page locally without saving the corresponding popup.js file. When he tries to open the html page, he'll get an error, since there is no popup.js available.

Is there a way to avoid this error? (in other words, is there a way to open the .html file without having the related .js file, and NOT getting an error?)

Thanks.

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 05-10-2002 23:08
code:
window.onerror = function() { return false; }


if you put that line in your script and the viewer won't get any message when there is an error in your script.

-= Veneficuz =-
"Mundus vult decipi. Ergo decipiatur."

viol
Maniac (V) Inmate

From: Charles River
Insane since: May 2002

posted posted 05-11-2002 01:37

The code provided doesn't work if IE 6 is configured to "Display a notification about every script error" and to not "Disable script debugging".
Is it supposed to be this way?
Thanks.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 05-11-2002 09:53

Actually, error handler function should return true instead of false. The correct code is:

window.onerror = function() { return true; }


bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-11-2002 10:27

Max's code will work fine.

Rule of thumb though, I don't code for browsers that use something outside the default settings. (as in your example).

personally if the file is not getting included you should find out why instead of repressing the errors.



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 05-14-2002 17:09

Huh! Would ya look at that.

Just what I was looking for. Works like a charm too!

Espically usefull when your triggering functions in one window that act on another window with setTimeouts. Trying to avoid errors if you closed one window when those timeouts are still running was starting to wear on me.

viol
Maniac (V) Inmate

From: Charles River
Insane since: May 2002

posted posted 05-14-2002 23:16

Yes, Max's code worked fine.
I like my pages without visible errors. While making them, I disable the line, so I'm aware of any error, then, when I'm going to post it, I enable the line.
Thanks!

hlaford
Bipolar (III) Inmate

From: USA! USA! USA!
Insane since: Oct 2001

posted posted 05-15-2002 18:36

The easiest way to avoid an error like that is to check for the function's existence first:

code:
<script language="JavaScript" type="text/javascript">if (Wpic) Wpic();</script>



bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-16-2002 00:37

That actually will error.

Testing for something that has not been defined will error in JS (works in some languages though)



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-16-2002 02:54

No. Testing for something that hasn't been defined in JS will return false.

The problem occurs when you treat something that hasn't been defined as an object, and check if one of its properties is defined; for example:

if (thisVariableDoesntExist) doSomething();
// works fine, it will *not* call the doSomething function.

if (thisVariableDoesntExist.inexistantProperty) doSomething();
// causes an error. can't access the property of an inexistant object.

This is why stuff like "if(document.layers) NN = true;" works.

viol
Maniac (V) Inmate

From: Charles River
Insane since: May 2002

posted posted 05-16-2002 04:07

hlaford's code results in error, in IE6. I didn't test it for other browsers.
I like the idea of a global and easy way to turn on and off the error checking (Max's code).

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-16-2002 05:18

I'm willing to bet that hlaford's code fails because even though the Wpic function exists, it's something inside of it that's causing the error.

In any case, if Max's code works, go ahead and use it. I for one very rarely use that code; it seems like a slightly irresponsible way of avoiding errors... I find places where errors might appear and then test against the problems. That way, I have control over what happens when there's a problem - otherwise, a piece of code might run halfway, and cause an error, and just stop abruptly, leaving something unfinished and looking strange to the user.

hlaford
Bipolar (III) Inmate

From: USA! USA! USA!
Insane since: Oct 2001

posted posted 05-16-2002 15:13

if you didn't like the first iteration, try checking typeof:

code:
if (typeof Wpic == "function") Wpic();


typeof will return "undefined" if Wpic has not been declared.

viol
Maniac (V) Inmate

From: Charles River
Insane since: May 2002

posted posted 05-16-2002 15:25

Wpic just opens a window and write to an htm file.

==========================

Contents of popup.js:

function Wpic(Pindex) {
if (window.document.bgColor == "#99ccff") Cor=1; else Cor=2;
avHTM = '<a href="JavaScript:void(0);" onclick="return popItUp(' + eval(Pindex) + ',' + Cor + ');" target="popJWin">';
avHTM += '<img border="1" src="thumbs/_' + eval("F" + Pindex) + '" align="right" hspace="5" vspace="3" width="180" height="135" Alt="Click me"></a>';
document.write(avHTM); }

function popItUp(pIndex,Kor) {
popUpWin = window.open('screenshot.htm?img=shots/'+eval("F" + pIndex)+'&caption='+eval("L" + pIndex)+'&cor='+Kor,
'popJWin','resizable=yes,scrollbars=no,menubar=no,width=535,height=420,left=5,screenX=5,top=5,screenY=5'); // pic is 500 x 375
popUpWin.resizeTo(535+12,420+38)
popUpWin.focus();
return false; }

==========================

Contents of screenshot.htm:

<html>
<head>
<title>Screenshot - MOHAA</title>
<style type="text/css">
<!--
TD {text-decoration:none;font-family:trebuchet ms;font-size:8pt;}
// -->
</style>
<script language="JavaScript" type="text/JavaScript" src="script/sshot.js"></script>
</head>
<script type="text/javascript">insertBody();</script>
<div align="center">
<center>
<table border="3" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="500">
<tr>
<td width="100%"><script type="text/javascript">insertImg();</script></td>
</tr>
</table>
</center>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="500">
<tr>
<script type="text/javascript">insertTDTag();</script>
<script type="text/javascript">insertCaption();</script></td>
</tr>
</table>
</div>
</body>
</html>

==========================

Contents of sshots.js (not originally written by me, just modified by me - used with permission):

/**
* Get data from a query string into a hash object
* @param encoded_data A url encoded string (like ?a=abc&b=xyz)
* @return a hash object of property/value pairs
*/
function extractQueryData(encoded_data)
{
var query_data = new Object;
var query_start = encoded_data.indexOf('?');
if(query_start != -1)
encoded_data = encoded_data.substr(query_start+1);
var data_array = encoded_data.split('&');
var i;
for(i=0; i < data_array.length; i++)
{
var data = data_array[i].split('=');
var prop = data[0];
var val = unescape(data[1]);
query_data[prop] = val;
}
return query_data;
}

var Query = extractQueryData(location.search);

/// Write out the image tag
function insertImg()
{
var imgsrc = Query["img"];
document.write('<img border="0" src="' + imgsrc + '" width="500" height="375">');
}

/// Write out the image caption
function insertCaption()
{
var caption = Query["caption"];
document.write(caption);
}

/// Write out the backgound tag/color
function insertBody()
{
var colori = Query["cor"];
if (colori == 1)
document.write('<body bgcolor="#B7DBFF">'); else
document.write('<body bgcolor="#d1dd99">');
}

/// Write out the TD font color tag
function insertTDTag()
{
var colori = Query["cor"];
if (colori == 1)
document.write('<td width="100%" style="color:#000080;">'); else
document.write('<td width="100%" style="color:#006000;">');
}


==========================

Contents of a sample html page that uses Wpic() - here is where the onerror function is located:

<html>
<head>
<title>Title goes here...</title>
<script language="JavaScript" type="text/JavaScript" src="script/popup.js"></script>
<script language="JavaScript" type="text/javascript">
<!--
window.onerror = function() { return true; }
var F1='name_of_a_file_1.jpg';
var L1='Pic. 1 - Legend 1';
var F2='name_of_a_file_2.jpg';
var L2='Pic. 2 - Legend 2.';
// -->
</script>
<link rel="stylesheet" type="text/css" href="script/green.css">
</head>
<body>
<p> blabla... </p>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%">
<tr><td width="100%" class="txt">
<script language="JavaScript" type="text/javascript">Wpic(1);</script>
<b>1.</b>&nbsp; Bla bla bla ... (pic. 1).</td></tr>
<tr><td width="100%">&nbsp;</td></tr>
<tr><td width="100%" class="txt">
<script language="JavaScript" type="text/javascript">Wpic(2);</script>
<b>2.</b>&nbsp; Bla bla bla ... (pic. 2)</td></tr>
</table>
<p> blabla... </p>
</body>
</html>

viol
Maniac (V) Inmate

From: Charles River
Insane since: May 2002

posted posted 05-16-2002 15:36

Here is a live sample of all the code above working: http://www.viol.net/mohaa/tips_resistance.htm


bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-17-2002 06:41

Serious Slime you can try that code as is it error in a normal setup IE browser. I've run into that a lot with popup trying to test for windows that are open that I've named. something like:

function openwin() {
myWin = window.open(..... etc.

}

if (myWin) myWin.location.href = url;
else openwin(url);


That has always fails if you try to run the if without having run the function first. Works fine though if you set mywin false early in the script.

I think it has to do with the lack of typecasting and variable definitions (the "$") in JS.



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-17-2002 08:07

You know what? you're right. How strange. I just tried it on my own page and it failed. However, this *did* work:

if (window.Wpic) Wpic();

I'm not sure why.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-17-2002 09:49

lol

That is funny. Silly programming languages



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

Yossi Admon
Bipolar (III) Inmate

From: Israel
Insane since: Nov 2001

posted posted 05-19-2002 07:43

Whay not to use the try catch (IE5+,Netscape6) to avoid errors?
It can be very useful to group code with one error handling.

try{
var a = docu.....

} catch(e){
alert("....");
}


« BackwardsOnwards »

Show Forum Drop Down Menu