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

About

Related Posts

Cisco VPN not Working on Windows 10

Error: Cisco VPN is not working (cannot login and connect to VPN connection). Solution: 1. Right-click on the VPN client, and Run as Ad...

About Me

My photo
Is an ordinary unNoticed 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. I like English and Chinese language, and know a little about them. Hence, for me, most things can best be expressed in Chinese, some in English, and the rest in others ^^

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

Follow by Email