Calling javaScript Function

326 views Asked by At

I am adding my external .js file as

<head>
    <script type="text/javascript" src="service/js/test.js"></script>
</head>

Inside the test.js I have one function called functionTest().

Calling this function onload of the body is working fine;

<body onload="functionTest()">

</body>

My Question is:

I want to call this function two time in the two different divs. How should I achieve this ?

<body>
    <div id="left">
        <!-- Directly Want to call functionTest() when div is getting loaded -->
    </div>
   <div id="right">
       <!-- Directly Want to call functionTest() when div is getting loaded -->
   </div>
</body>
2

There are 2 answers

0
Tushar On BEST ANSWER

Add the call to the function at the end of the div. The function functionTest will be called when the div is completely loaded.

<body>
    <div id="left">
        <!-- Directly Want to call functionTest() when div is getting loaded -->

        <script>functionTest();</script>
        <!--    ^^^^^^^^^^^^^^^ -->
    </div>
    <div id="right">
        <!-- Directly Want to call functionTest() when div is getting loaded -->

        <script>functionTest();</script>
        <!--    ^^^^^^^^^^^^^^^ -->
    </div>
</body>
0
Fabjan On

Just insert <script> functionTest() </script> inside each div