Tuesday, July 31, 2012

Check if a Function Exists in Javascript

There are times when we want to call a JavaScript function, and in certain circumstances, we want to make sure that the function does exist. e.g. When a dialog window can be opened from more than one page. And, we want to refresh the component of the the parent page from the dialog window. Thus, we need to check if the function really exists.

This is a simple yet useful example to check if a function exists in JavaScript.


<input type="button" onclick="checkFunctionExist();" value="Check Function Existence" />

<script type="text/javascript" language="javascript">

 function testFunction() {

 }

 function checkFunctionExist() {
  if (window.testFunction) alert('testFunction exists');
  else alert('testFunction does not exist');

  if (window.nonExistantFunction) alert('nonExistantFunction exists');
  else alert('nonExistantFunction does not exist');
 }

</script>


To check whether a function exists on the parent page (the page which opened the dialog window), use:

if(window.opener.testFunction) alert('testFunction exists');
else alert('testFunction does not exist');

Share:

0 comments:

Post a Comment

You may be intersted in

Related Posts

Updating Table Containing Xml Column via LinkedServer

If you are trying to update a table containing XML column via Linked Server in SQL Server, and you are not able to, you are not alone. There...

About Me

My photo
Is an ordinary man, with a little knowledge to share and high dreams to achieve. I'd be glad if I can help others, 'coz the only thing for the triumph of evil is for a good man to do nothing.

About Blog

You can find a lot of debugging and deploying problems while developing applications in .NET and Visual Basic here. There are also some querying tips in SQL and typical source codes which might be useful shared here.

Popular Posts

Blogroll

Followers

Leave a Message