When the window loads, 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("方法不存在"); } } function test(){ alert("我是test()方法"); } </script>