Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Window onError

Status
Not open for further replies.

AlanDI1

Programmer
Apr 20, 2003
56
Hi -

I have the following code that links to a specific location on my server. But the linked to location does not always exist and because of the way the link name is built I will not know that until the user clicks the link they want to go. So I have:

<script language="javascript">
// use java to check for existence of file then call url
function Graph_OnClick(urlPart1, theItem, urlPart2) {
window.onError = GraphCallError() ;
window.location.href (urlPart1 + theItem + urlPart2);
}
// location href failed so alert user
function GraphCallError (msg, url, linenumber) {
alert('Graph File Was NOT Found!');
}

</script>

The OnClick fires fine but the onError ALWAYS fires (whether or not the link exists) displaying the alert then the location.href fires regardless of whether or not the link exists.

What am I missng?

Thanks for you help
 
The highlighted portion is your problem.
Code:
function Graph_OnClick(urlPart1, theItem, urlPart2) {
    window.onError = GraphCallError[!]()[/!] ;
    window.location.href (urlPart1 + theItem + urlPart2);
  }

Imagine that GraphCallError was a function defined as so:
Code:
function GraphCallError() {
   return 1;
}

If this was the case, then when this command is ran:
Code:
window.onError = GraphCallError() ;
GraphCallError() runs and returns 1. It's essentially the same thing as saying this:
Code:
window.onError = 1;

Now, since javascript functions are actually objects, you leave off the parentheses when assigning a function to an event handler. What that says is, when an error occurs run the function, like this:
Code:
window.onError = GraphCallError;

-kaht

Google is gobbling up the Internet - electricphp

Finally, <. is a good thing!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top