How to trap an error?

D

Dave

Hi guys,

I have the following JS which is erroring and I don't know why.
Furthermore, it errors by hanging during runtime so I am unable to
determine what is wrong.

The id exists and is viewable in the source code of my web page. Any
suggestions?

this.window.document.getElementById('form_495').reset();

alert(this.window.document.getElementById('form_495')); <-- returns an
object

Thanks in Advance,
Dave
 
R

RobG

Hi guys,

I have the following JS which is erroring and I don't know why.
Furthermore, it errors by hanging during runtime so I am unable to
determine what is wrong.

The id exists and is viewable in the source code of my web page. Any
suggestions?

this.window.document.getElementById('form_495').reset();

In some browsers:

this.window === window

is true, in others it's false. Unless you have a really good reason
for using it, don't.

Also, global variables like document don't need to be prefixed with
"window". Contrary to popular belief, it doesn't speed up identifier
resolution (even if it did, the effect is miniscule).

Lastly, your error might be because form_495 doesn't exist (though
that doesn't seem to be your problem) and calling a method if a non-
existent object will cause your script to fail. So:

var form = document.getElementById('form_495');
if (form && form.reset) form.reset();

should do the trick, or at least not error (provided that you've
previously tested for document.getElementById) :)
 
D

Dave

Hi Rob,

Firstly, thank you for your informative answers.

I used the below statements and statement no longer errors. This
dosn't actually solve my problem though...I need to call the forms
reset() method so I need to find out why form.reset() won't work. Any
more suggestions?
var form = document.getElementById('form_495');
if (form && form.reset) form.reset();

Furthermore, I attempted the following which still errors - hangs

//var form = document.getElementById('form_495'); <-- if i use this to
become the value of the form variable, the below 'if' statement does
not equate to true...?
var form = this.window.document.getElementById('form_495');
if (form) form.reset();

Thanks in advance,
Dave
 
R

RobG

Hi Rob,

Firstly, thank you for your informative answers.

I used the below statements and statement no longer errors. This
dosn't actually solve my problem though...I need to call the forms
reset() method so I need to find out why form.reset() won't work. Any
more suggestions?

That's why the code is written that way - it stops errors of logic
(that is, where what you expect to happen doesn't) from causing fatal
syntax errors (such as calling a method of a non-existant object).
Furthermore, I attempted the following which still errors - hangs

//var form = document.getElementById('form_495'); <-- if i use this to
become the value of the form variable, the below 'if' statement does
not equate to true...?
var form = this.window.document.getElementById('form_495');

Why the second assignment to form? That will change its value -
comment out the second assignment (or maybe you posted it in error).
if (form) form.reset();

That is a poor test, it doesn't test what you are looking for (i.e.
whether an element with id=form exists and whether it has a reset
method). Go back to the original code I suggested.

I suspect you are trying to access the form before it's loaded into
the document. Try placing your script block after the form's closing
tag, or run the function using window.onload:

window.onload = function(){
var form = document.getElementById('form_495');
if (form && form.reset) form.reset();
}
 

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
474,159
Messages
2,570,880
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top