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
Swetha A 5Swetha A 5 

How to add JSENCODE script to a jvascript Onclick function?

Hi All,
I want to add JSENCODE to my visualforce page where I have a javascript function and it is used in one of my statements in the code. Below is my code:
<a style="padding-left:5px;color:black;" id="link" href="javascript:void(0);" onclick = "changeStage('{!Values.Stage_Value__c}');">{!Values.Stage_Value__c}</a>
And the javascript function is: 
function changeStage(changeStageName) {
        
        stagename(changeStageName);
     }
Can Somebody help me in solving this!! Any help or suggestion is appreciable.
Thanks in advnce

 
Best Answer chosen by Swetha A 5
Rahul KhilwaniRahul Khilwani
You can replace your code with the code below:
 
changeStage('{!JSENCODE(Values.Stage_Value__c)}');
Let me know if this works.

All Answers

ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Sweta,

your requirement is not so clear to me. Can you explain me little more. 

this example may help you.

<script>
var text = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';

obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
</script>

let me know, if it helps you or need any help :)

shiva.sfdc.backup@gmail.com
Swetha A 5Swetha A 5
hi Shiva Krishna..
Actually When I scanned my code in code scanner, it shows error that "Object: userenginecontroller in file: components\CurrentUserSwipeComponent.component" . CurrentUserSwipeComponent is my visualforce component  where I have the above code. In place of Onclick ="changeStage('{!Values.Stage_Value__c}'", I need to add JSENCODE . I dont have any idea how to script there.
Could you please help me. Thank you for your quick reply.
Swetha A 5Swetha A 5
Actually When I scanned my code in code scanner, it shows error that "Object: userenginecontroller in file: components\CurrentUserSwipeComponent.component" . CurrentUserSwipeComponent is my visualforce component where I have the above code. In place of Onclick ="changeStage('{!Values.Stage_Value__c}'", I need to add JSENCODE . I dont have any idea how to script there. Could you please help me. Thank you for your quick reply.
Swetha A 5Swetha A 5
changeStage('{!Values.Stage_Value__c} : ChangeStage is my Javascript function Name, and Stage_Value__c is my field name.
Please help in fixing this issue.
Rahul KhilwaniRahul Khilwani
You can replace your code with the code below:
 
changeStage('{!JSENCODE(Values.Stage_Value__c)}');
Let me know if this works.
This was selected as the best answer
Swetha A 5Swetha A 5
Thanks Rahul.. It worked.
Vilas DhobleVilas Dhoble
Hi Swetha,

Your formula expression - that is {!Values.Stage_Value__c} is the place where you need to use encode function.
Generally, JSENCODE() is needed inside Scripts but here you are having an event inside the HTML tag <a>..</a>
For HTML tags we need HTMLENCODE()

So in your case, it should be as below -
<a style="padding-left:5px;color:black;" id="link" href="javascript:void(0);" onclick = "changeStage('{!JSENCODE(Values.Stage_Value__c})');">{!Values.Stage_Value__c}</a>

Note : JSENCODE(HTMLENCODE()) is equivqlent to JSINHTMLENCODE()

Refer : 
1. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/pages_security_tips_scontrols.htm
2. https://developer.salesforce.com/docs/atlas.en-us.secure_coding_guide.meta/secure_coding_guide/secure_coding_cross_site_scripting.htm

Mark this as the best answer if it is. Happy Learning.