function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BroncoBoyBroncoBoy 

<apex:commandLink> action and onclick Attributes Conflicting?

I have a VF page referencing a controller.  In the page I have a <apex:commandLink> component where I'm trying to use the action attribute to execute a controller method and the onclick attribute to execute some JavaScript (a jquery dialogue that shows a video) - simultaneously.  The purpose:  to track link clicks and store a record in a custom object for reporting purposes.  When I put both attributes (action & onclick) in the <apex:commandLink> component, only the JavaScript executes.  If I take out only the JavaScript the action/controller method works fine and a record is created in my custom object.  Any thoughts as to a workaround or cause?  Thank you in advance!

 

Here's my code

 

VF Page Code:

<apex:pagecontroller="LinkInformationController">

  <script>

  var content2;

var counter2 = 0;

function showDialog(url){

  $ = jQuery.noConflict();

   

  if (counter2 !== 0) {

        content2.appendTo("body");

    }

    $("#divId").dialog({

          autoOpen: false,

          modal: true,

          height: 375,

          width: 500,

          close: function () {

          $(this).dialog('destroy');

                        content2 = $("#divId").remove();

                        counter2 = counter2 +1;

        }

      });

  $("#divId").html('<iframe id="modalIframeId" width="100%" height="100%" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" />').dialog("open");

  $("#modalIframeId").attr("src",url);

  return false;

}

  </script>

  <apex:includeScriptvalue="{!$Resource.jquery19}"/>

  <apex:includeScriptvalue="{!$Resource.jquery19ui}"/>

<apex:pageBlocktitle="Training Videos">

        <apex:pageBlockSectiontitle="Click on the link">

<apex:form>

  <table width="100%">

  <tr>

  <td class="lnks">0)<apex:commandLink value="Test Video" action="{!myMethod}" onclick="return showDialog('URL to Video')">

<apex:paramname="myParam"value="Video 1"/>

</apex:commandLink></td>

  </tr>

</table>

</apex:form>

</apex:pageBlockSection>

</apex:pageBlock>

</apex:page>

 

Controller Code:

 

public class LinkInformationController {

 

public LinkInformationController(){

 

}

 

public void myMethod() 

{

  string myParam = apexpages.currentpage().getparameters().get('myParam');

  Link_Information__c newLI = newLink_Information__c

(

Date_Link_Clicked__c = date.today(),

Link_User_Id__c = UserInfo.getUserId(),

Link_Name__c = myParam,

Link_User_Name__c= UserInfo.getName()

);

insert newLI;

}

}

Best Answer chosen by Admin (Salesforce Developers) 
firechimpfirechimp

Hi BroncoBoy,

This is because you have used the return key word in your onclick event.

 

What may be best though is moving the onclick event to the on complete, this will ensure you log the click before showing the video (though this may cause a little delay).

 

Thank you

All Answers

firechimpfirechimp

Hi BroncoBoy,

This is because you have used the return key word in your onclick event.

 

What may be best though is moving the onclick event to the on complete, this will ensure you log the click before showing the video (though this may cause a little delay).

 

Thank you

This was selected as the best answer
BroncoBoyBroncoBoy

Sorry for the delay in response but - it worked, thank you so much! 

 

May I ask how you were able to determine the cause?  Trying to learn...

firechimpfirechimp

Hi BroncoBoy,

 

Glad I could help.

 

This is one of those things that you just pickup over time. If you want to learn something that would help you to spot things like this earlier, I would suggest learning about visualforce execution order and javascript events.

 

Hope that helps!