[SalesForce] Call a JavaScript function into another JavaScript function in a Visualforce page

I have a main page, with a main controller, that contains some JavaScript code:

<apex:page controller = "controller1">
  <script>
    function function1(){
     //I want to call the function 2 here
    }
  </script>
</apex:page1>

And a component with some JavaScript code too:

<apex:component controller="controller2">
 <script>
   function2(){ //Some code here }
 </script>
</apex:component>

Well, I want to call the function 2, contained in the component, into my main page, like in the code above…

Is it possible? If yes, how?

The function2 in my component's JavaScript code refers to a method in my controller2, but I don't know if this information is useful for this problem.

Best Answer

We can access directly.

<apex:page controller="OppsController">
    <apex:form >
        <c:Test></c:Test>
        <input type="button" value="test" onclick="test();"/>
        <script>
        function test(){ 
        function2();
        }
        </script>
    </apex:form>
</apex:page>

Component

<apex:component >
    <script>
   function function2(){
   alert('in component');
   }
 </script>
</apex:component>
Related Topic