Reputation: 535
How can I call a controller method from VisualForce page without any onclick event happening? My VisualForce page would be like
<apex:page standardController="Account">
<script>
/* Here i need to call a controller class with out any onclick event. It should load by itselef */
</script>
</apex:page>
and my controller class will be like
public class OrderPadController {
/* this will be the constructor */
public PageReference openPage() {
PageReference newpage = new Pagereference('/apex'+'/pagetoopen'+'?aid='+a.id);
openorderpadembed.setRedirect(false);
return openorderpadembed;
}
From my VisualForce page I need to call the method openPage()
.
Please help me out
Upvotes: 3
Views: 21140
Reputation: 19637
<apex:page standardController="Account" extensions="OrderPadController"
action="{!openPage}">
</apex:page>
the action parameter will call the method you want as soon as the page loads. It is a dangerous attribute, one that will be pointed out to you if you'll go through Salesforce's security review process. The called action can manipulate database values for example (whereas the OrderPadController constructor can't) and the philosophy is that act of visiting a page shouldn't have any side effect.
In your specific scenario you can do it even without any Apex code at all:
<apex:page standardController="Account"
action="/apex/Gantt?aid={!$CurrentPage.parameters.id}">
Look Mom, no hands!
</apex:page>
As Pavel mentions - please write more about your requirement. I suspect that you don't even need this visualforce page acting as redirect, that a simple custom link or button (type = url instead of Visualforce or Javascript) could do the trick...
Upvotes: 1
Reputation: 3585
You can use JavaScript Remoting for Apex Controllers in the following manner:
<apex:page standardController="Account" extensions="OrderPadController">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
window.$j = jQuery.noConflict();
$j( document ).ready(function() {
OrderPadController.openPage(function(result, event){
console.log(result);
window.open(result,"_self");
});
});
</script>
</apex:page>
public class OrderPadController {
//
@remoteAction
public PageReference openPage() {
PageReference newpage = NEW Pagereference('/apex' + '/pagetoopen' + '?aid=' + a.id);
openorderpadembed.setRedirect(false);
return openorderpadembed;
}
}
Upvotes: 4