calling visualforce page from javascript

Many times we come across requirement where-in we have to call a particular Visualfroec page upon confirming. Such a requirement can be accomplished by using java script. A visualforce page can be called from java script.
For this we have to use window.open as used in the example below:

Visualforce page that calls another visualforce page upon confirming from java script.

<apex:page standardController="Contact" >
    <apex:form >
        <apex:pageBlock >
            <apex:commandButton value="Open vf Page" onclick="OpenVfpageConfrim()"/>
        </apex:pageBlock>
    
    <script>
      function OpenVfpageConfrim(){
        var confirmFlag =confirm('Do you want to open a new visualforce page?');
        if(confirmFlag == true)
           window.open('/apex/YourNameOfVisualforcePage');
      }
     </script>
    </apex:form>
</apex:page>

Similarly, if you want to open a visualforce page from a custom button using java script then use following piece of code.
Override the custom button with below java script.

  {!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
    var confirmflag = confirm('Do you want to open new Visualforce page?');
    if(confirmflag == true)
      window.parent.location.href="/apex/YourNameOfVisualforcePage";

2 comments:

  1. Thanx ... well explain in easy way... thanx for you are post..Yoganand

    ReplyDelete
  2. how to call apex method from javascript button

    ReplyDelete