0

I want to know if I can use javascript in place of apex:page action. I am implementing js remoting in my vf page. I am going to add @remoteaction before my pagereference method in controller. I also know how to call a @remoteaction method in vf page using javascript for implementing js remoting. However, I am not sure how i can replace action in the apex:page tag which gets called when the vf page loads and performas redirection based on who the logged -in user is (this logic is handled in pagerefernce method in controller).

1

You'll probably want to use an apex:actionFunction instead of a @RemoteAction. This is because the latter does not pass the page state to the server, which may have undesirable results. You can do so with something like this:

<script>
window.addEventListener('DOMContentLoaded', checkRedirect, false);
</script>
<apex:form id="form"> <!-- required for apex:actionFunction -->
    <apex:actionFunction name="checkRedirect" action="{!checkRedirect}" reRender="form" />

Note that using apex:page action is probably a better deal for the user, because less data is transferred to the user's browser, so the redirect occurs faster and with less wait time for the user.

If you choose to use @RemoteAction, then you have to perform the redirect yourself, like so:

{!$RemoteAction.controllerName.functionName}(
    function(data, event) { 
       if(data) { window.top.location.href = data; } 
    });
  • We are facing some performance issues on our VF pages and when Salesforce reviewed our pages, they suggested to use JS remoting. The only place where controller method is called is in action of apex:page (which does the redirection based on which profile user belongs to). In all the other places, VF pages calls controller properties. As you mentioned in your answer that remoteaction will be slower than apex:page action, i am bit confused. – TJD Oct 22 '15 at 21:27
  • Anyways, I came across this post which describes how to use javascript for page load but don't know how effective it is for performance: stackoverflow.com/questions/21047775/… – TJD Oct 22 '15 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.