You need to sign in to do that
Don't have an account?

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:
Thanks in advnce
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
Let me know if this works.
All Answers
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
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.
Please help in fixing this issue.
Let me know if this works.
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.