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
saleforce beesaleforce bee 

command button with javascript issue

<apex:page sidebar="false" showheader="true">
<script type="text/javascript">
function link()
{
window.location= 'https://c.ap1.visual.force.com/apex/helloworld';
}
</script>
<apex:form id="new">
	<apex:commandButton styleClass="groovybutton" onclick="link();" id="button1" value="commond button"/>
	<input type="button" name="groovybtn1" onclick="link()" class="groovybutton" value="HTML button"/>
</apex:form>
</apex:page>

 

 

In the above code snippet


when i click the command button , page redirect is not happening. However, i click HTML button page redirect is happening.

please help me to fix it



 

Best Answer chosen by Admin (Salesforce Developers) 
Alok_NagarroAlok_Nagarro

Hi,

 

<apex:commandButton> is a action component and expect to execute controller method in response to click event, if you dont give action attribute then it will simply refersh the page. To prevent this behaviour just return false from onclick event like -

 

<apex:commandButton styleClass="groovybutton" onclick="link();return false;" id="button1" value="commond button"/>

Hope it would work.

All Answers

Alok_NagarroAlok_Nagarro

Hi,

 

<apex:commandButton> is a action component and expect to execute controller method in response to click event, if you dont give action attribute then it will simply refersh the page. To prevent this behaviour just return false from onclick event like -

 

<apex:commandButton styleClass="groovybutton" onclick="link();return false;" id="button1" value="commond button"/>

Hope it would work.

This was selected as the best answer
saleforce beesaleforce bee

Works.Thanks for quick response Alok