+ Start a Discussion
bs881026bs881026 

Function on Pageload

What are the ways to call a function on the Page Layout apart from the constructor calling.

Kindly help me on this.

Thanks.
Best Answer chosen by bs881026
Rupali PophaliyaRupali Pophaliya
There can be following ways:

1. Using javascript Onload function:
 
<script type = "text/javascript">
    window.onload=function()       
    {        
        alert("Hello");
    };
</script>



2. Visualforce page Action:
 
<apex:page action="{!onLoad}">

</apex:page>

 

All Answers

Rupali PophaliyaRupali Pophaliya
There can be following ways:

1. Using javascript Onload function:
 
<script type = "text/javascript">
    window.onload=function()       
    {        
        alert("Hello");
    };
</script>



2. Visualforce page Action:
 
<apex:page action="{!onLoad}">

</apex:page>

 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
hi bs881026,

If you are talking about VF page then please check below post. I hope that will help you
1) http://amitsalesforce.blogspot.in/2015/04/visualforce-page-life-cycle-in.html

Order of executetion in VF page
1) Constructor of controller /extension - >  
2) constructor of custom components  ->
3) AssignTo any Custom Components ->
4) Action method
5) Getter or Setter
6) Form method -> HTML

So After constructor  you are good to go with Action method.
1) https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_page.htm
ApexPages.Action
The action method invoked when this page is requested by the server. Use expression language to reference an action method. For example, action="{!doAction}" references the doAction() method in the controller. If an action is not specified, the page loads as usual. If the action method returns null, the page simply refreshes. This method will be called before the page is rendered and allows you to optionally redirect the user to another page. This action should not be used for initialization.

Let us know if this will help you
Shubham NandwanaShubham Nandwana

Hi @Rupali, @Amit,
Is there a way we could pass a variable with the action method in <apex: page> tag?

Suppose I have a method onLoad:
 

public void onLoad(String test){
System.debug(test);
}

I want it to be called on page load with a default value passed in the action. How can I accomplish that?
Thanks