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.
To check whether a function exists on the parent page (the page which opened the dialog window), use:
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');