I have a visual force page with a custom class behind it. This page has only one button. When you click anywhere on the page the method associated with the button executes. How do I prevent that from happening. If someone wants to highlight some text and they click anywhere on the page by mistake the apex class method associated with the button fires and do not want that to happen unless they specifically click the button

i used a command button to begin with but couldn't cause it to open in a new window so I changed it to an html input button but I don't think that should be the default behavior for that either

<apex:page standardstylesheets="false" controller="displaycase" action="{!ShowDetail}" sidebar="false" >
    function openNewVFPage() {
      var currentUrl = document.URL;
      var caseNo = '';
      var nPos = currentUrl.indexOf("?cn=");
      if (nPos >= 0) {
        caseNo = currentUrl.substr(nPos + 4, 8);
        var vfPageUrl = "/support/apex/CCDetailPrint?cn=" + caseNo;
      } else { alert('Cannot find case number to generate print view'); }
    document.title = "Service Portal";
<apex:form id="mcpFrm">
  <apex:outputLabel id="detail" >
    <apex:pageBlock rendered="{!if(!disp,false,true)}">
      <apex:pageBlockButtons location="top">
     <!--     <apex:commandButton id="pv" value="Print View" onclick="openNewVFPage();"/>   -->
     <input type="button" value="Print View" onClick="openNewVFPage();" />
   <h3><table style="width:100%">
  • 3
    Please post your VF page markup so we can better understand what is going on. Btw, that's not the default behavior, so something must be goofy – RenegadeCoder Mar 2 '16 at 21:41
  • Even creating the url in my class method and using and apex:button makes no difference I can click anywhere on the page and it acts like I clicked the button and I don't want that to happen. <apex:commandButton id="pv" value="Print View" onclick="window.open('{!pvUrl}','_blank');"/> – Ohmicron Mar 3 '16 at 13:55

You may need to play around with the styling some more to make it properly appear as a button, but try this:

<apex:commandLink target="_blank" value="Print View" styleClass="btn" action="{!URLFOR($Page.CCDetailPrint, null)}" >
    <apex:param name="cn" value="{!$CurrentPage.parameters.cn}" />
  • 1
    You totally rock! The first defined commandButton seems to be the default button for clicking anywhere in the PageBlock that it is defined in. But that is definitely not the case with commandLinks and I just needed to style the link to look like a button. Awesome! Thanks Renegade – Ohmicron Mar 3 '16 at 18:50

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.