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
EIE50EIE50 

Basic question on Javascript in apex:commandlink

Hi,

 

I would like to use a javascript function to email the url, and i am just wondering how can i call the java script function using apex:commandlink?Can anyone help me with this? The below way of passing the javascript doesnt work, the link is just static when clicked, no action is occuring.

 

 

<apex:commandLink style="float:right" onclick="{!$Resource.EmailLink}" value="EmailLink"/>

 

 

i am posting my Javascript here:

 

 

function EmailLink(){
window.location = "mailto:"+"?subject=I thought this link might interest you." + "&body="+document.title+"  "+window.location;
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bmabma

You would have to include the javascript on the page before calling it.

 

 

<apex:includeScript value="{!Resource.file_name}"/>

<apex:commandLink style="float:right" onclick="EmailLink();" value="EmailLink"/>

 

This should call the javascript function EmailLink().

 

All Answers

bmabma

You would have to include the javascript on the page before calling it.

 

 

<apex:includeScript value="{!Resource.file_name}"/>

<apex:commandLink style="float:right" onclick="EmailLink();" value="EmailLink"/>

 

This should call the javascript function EmailLink().

 

This was selected as the best answer
EIE50EIE50

Hi,

 

your solution did work, but whenever i click on the email link the page refreshes and i dont want it to happen. Since i did not specify action attribute, the page is getting refreshed, What am i suppose to write in the code (apex), so that the page does not refresh everytime when i click email link.

 

Thanks.

Ispita_NavatarIspita_Navatar

Did you try returning null and have reuturn type as pagereference?

EIE50EIE50

Hi,

 

I tried the following code.

 

public PageReference noAction() { 
   
return null; 
        
} 

 

and in VF page:

<apex:commandLink action={!noAction} onclick="EmailLink()" ondblclick="EmailLink()" value="EmailLink" />

 

Was i wrong?

 

Thanks.

ahab1372ahab1372

Maybe you should not use apex:commandLink, but create the link manually with <a onclick="...">linkText</a>

commandlink will always refresh unless you specify the rerender attribute. Not sure if you could point the reRender to something insignificant and thus avoid the reload

EIE50EIE50

Thanks, i used <a>tag and specified required attributes in it and this works fine now.