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 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()"/>
      function OpenVfpageConfrim(){
        var confirmFlag =confirm('Do you want to open a new visualforce page?');
        if(confirmFlag == true)

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.

    var confirmflag = confirm('Do you want to open new Visualforce page?');
    if(confirmflag == true)


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

  2. how to call apex method from javascript button