Comment JavaScript Code On Visualforce Page

Comment JavaScript Code On Visualforce Page

 Hi Visualforce developers, I have faced one problem on JavaScript code in Visualforce page. and I have searched solution over the web, but I couldn't able to get solution. In this post I am going to explain that problem and the solution.

Problem: 

             While I was implementing the VF page, I have implemented different function in JavaScript Code under <Script> </Script> tag in Visualforce page.Because of some reason I have commented some JavaScript function  as we comment like apex Visualfocrce page code (<!-- Code -->).
              Because of this whenever the JavaScript function get called from apex tag, I am getting

the below error in JavaScript Console.(To know about JavaScript Console click here ).

 

Javascript Error: Uncaught ReferenceError: [function] is not defined in visualforce page.

Solution: 
           
            If you face this JavaScript error on Visualforce page then there is a  'Syntax' error on your 
JavaScript code it may be in any of the function inside the <Script></Script> code.

Visualforce Code:

<span style="color: red;"><span style="color: black;"><apex:page title="JavaScript code Comment" sidebar="false" >
     <apex:form >
         <apex:pageBlock title="Comment JavaScript code on Visualforce page">
         <apex:pageBlockButtons location="bottom">
             <apex:commandButton value="Show welcome message" onclick="displayMessage()"/>
         </apex:pageBlockButtons>
         </apex:pageBlock>
     </apex:form>
     
     
     <script>
        
         function </span></span><span style="color: red;"><span style="color: black;"><span style="color: red;"><span style="color: black;">displayMessage</span></span>(){
             alert('Welcome to salesforce dev Stuff blog');
         }
     </script>
</apex:page></span></span>

 

Output : 

In the above code I am going to add one more function and I going to comment that as we comment like a Visualforce page.

<span style="color: red;"><span style="color: black;"><apex:page title="JavaScript code Comment" sidebar="false" >
     <apex:form >
         <apex:pageBlock title="Comment JavaScript code on Visualforce page">
         <apex:pageBlockButtons location="bottom">
             <apex:commandButton value="Show welcome message" onclick="</span></span><span style="color: red;"><span style="color: black;"><span style="color: red;"><span style="color: black;">displayMessage</span></span>()"/>
         </apex:pageBlockButtons>
         </apex:pageBlock>
     </apex:form>
     
     
     <script>
        <span style="background-color: #cc0000;"> <!--</span></span></span>
 
<span style="background-color: #cc0000;">          function welcomeMessage(){
             alert('Welcome');
         }</span>
 
<span style="color: red;"><span style="color: black;"><span style="color: red;"><span style="color: black;"><span style="background-color: #cc0000;">          --></span> </span></span></span></span>
 
<span style="color: red;"><span style="color: black;"><span style="color: red;"><span style="color: black;"> </span></span>         function </span></span><span style="color: red;"><span style="color: black;"><span style="color: red;"><span style="color: black;">displayMessage</span></span>(){
             alert('Welcome to salesforce dev Stuff blog');
         }
     </script>
</apex:page></span></span>

 

Output:

To Comment the JavaScript  code in visualforce the methods are as follow as
             
           1.Single line comments start with 

eg:

<script>
         
                  //function welcomeMessage(){
                 //  alert('Welcome');
                 //}
         
                  function displayName(){
                      alert('Welcome to salesforce dev Stuff blog');
                  }
            </script>

 

2.Multi line comments start with /* and end with */

           
            eg.

<script>
         
                 /* 
                 function welcomeMessage(){
                   alert('Welcome');
                 }
                */
         
                  function displayMessage(){
                      alert('Welcome to salesforce dev Stuff blog');
                  }
            </script>

 

If you comment your JavaScript code either one of the above methods then your Visualforce JavaScript code will not throw any error like 'Javascript Error: Uncaught ReferenceError: [function] is not defined'. This Error definition will differ based on browzer.
         I think now you all are clear about how to comment JavaScript on Visualforce page.

Rating: 
No votes yet