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
arabisaysarabisays 

JavaScript in Visualforce Page

Hi. I am new to sales force. I have to perform a simple task.
When clicking an apex command button , it should alert a message through JavaScript.
Please see my code.

<apex:page standardController="Book__c">
<script>
    alertSample(){
        alert("Hello");
    }
</script>
    <apex:form >
        <apex:commandButton oclick="alertSample()" value="Hello"/>
        <apex:PageBlock title="Book View">
            <apex:outputField value="{! Book__c.Name}"/>
            <apex:detail />
        </apex:PageBlock>
    </apex:form>
</apex:page>

But I am not getting expected response. Please help.
Best Answer chosen by arabisays
Sri549Sri549
Hello raoof,

use this code you wlll get the alert as you like.
<apex:page standardController="Book__c">
<script>
    function alertSample()
    {   
        alert("Hello");
   
    }
</script>
    <apex:form >
        <apex:commandButton onclick="alertSample();" value="Hello"/>
        <apex:PageBlock title="Book View">
            <apex:outputField value="{!Book__c.Name}"/>
            <apex:detail />
        </apex:PageBlock>
    </apex:form>
</apex:page>

if you dont get requirment reply me so that i will ahelp you
 or else if you reach ur solution.

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer







All Answers

dev_sfdc1dev_sfdc1
Hi,
You have missed out to call as 'function' before giving function name.

<script>

   function alertSample() {
 alert("Hello");
}
</script>
Sri549Sri549
Hello raoof,

use this code you wlll get the alert as you like.
<apex:page standardController="Book__c">
<script>
    function alertSample()
    {   
        alert("Hello");
   
    }
</script>
    <apex:form >
        <apex:commandButton onclick="alertSample();" value="Hello"/>
        <apex:PageBlock title="Book View">
            <apex:outputField value="{!Book__c.Name}"/>
            <apex:detail />
        </apex:PageBlock>
    </apex:form>
</apex:page>

if you dont get requirment reply me so that i will ahelp you
 or else if you reach ur solution.

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer







This was selected as the best answer