• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1457
  • Last Modified:

javascript conditional onload - if the function is present...

This should be really easy, but I just can't seem to find the answer at the moment..

Consider onload event of the body tag -> if the BodyOnLoad function is in the page (either in the header or body), I would like to run it. If not do nothing (I stuck an alert in but doen't have to be there).

I may want to have BodyOnLoad in a separate js file, or right at the bottom, or not at all...

Many thanks in advance!
<html><head>
<script language="javascript" type="text/javascript"  > 
function BodyOnLoad()
{alert('1b');}
</sctipt>
</head>
<body onload="try{BodyOnLoad();}catch(e){alert('do nothing')}">
some text here...
</body>
</html>

Open in new window

0
CallConnection
Asked:
CallConnection
  • 2
2 Solutions
 
Ashish PatelCommented:
Try this way/.
<html><head>
<script language="javascript" type="text/javascript"  > 
function BodyOnLoad()
{alert('1b');}
</sctipt>
</head>
<body onload="if(!BodyOnLoad){alert('do nothing')}">
some text here...
</body>
</html>

Open in new window

0
 
Ashish PatelCommented:
Oops a small mistake. Please try this
<html><head>
<script language="javascript" type="text/javascript"  > 
function BodyOnLoad()
{alert('1b');}
</script>
</head>
<body onload="if(window.BodyOnLoad){ BodyOnLoad();} else {alert('do nothing');}">
some text here...
</body>
</html>

Open in new window

0
 
Pratima PharandeCommented:
Try this

You spell check for </sctipt> correct is
</script>

<html><head>
<script language="javascript" type="text/javascript"  >
function BodyOnLoad()
{alert('1b');}
</script>
</head>
<body onload="try{BodyOnLoad();}catch(e){alert('do nothing')}">
some text here...
</body>
</html>
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now