SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Using the Explorer Solitaire library : Overloading the Completion handler

The Check4Completion function present in the Explorer Solitaire library is a very useful function. It is called after every move that the user makes, and is used to detect if the game is over (i.e. all the foundations are full). You can hook your own custom code onto this function by using a technique called Function Overloading.

If you went through the second SCRIPT block in the previous page carefully, you will have noticed the onreadystate attribute set to point to the CustomScript function. This function is just a stub, it contains no code, but you can use this function to override the default behaviour of the Check4Completion handler. This is the code you would use :

function CustomScript(){
OldCheck=Check4Completion;
Check4Completion=NewCheck;
}

If you are a regular reader of SiteExperts, you will notice the use of function pointers here. The pointer OldCheck is used to store the code of the default Check4Completion handler. Then the handler is set to point to another function, NewCheck, which you have defined in your web page. So now, whenever the code in the library calls the Check4Completion handler, it is actually calling your custom handler! What's more, you don't lose the functionality of the default handler, you can still call it from within your custom handler by using it's new name OldCheck. You can have your cake and eat it too!

We won't be going into the details of the NewCheck function, as it manipulates structures internal to the library, and requires an understanding of how the library works. We will be discussing the library in detail in a future article. Till then, you can collect your dues from Mr. Conner, put your feet up and play the game.

Discuss and Rate this Article