When the window is loading, use typeof to determine whether the function exists in the context
<script type="text/javascript"> window.onload = function(){ try{ if(test && typeof(test) == "function"){ test(); } } catch(e){ alert("The method does not exist"); } } function test(){ alert("I am the test() method"); } </script>