I know this is going to be something simple that I'm just missing somehow, but here it goes:
I have a ColdFusion application where a user can enter text on multiple screens. I have a javascript function that checks the entered text against what is stored in the database and pops up a confirmation window asking them if they want to save/discard changes.
If the user-entered text contains quotes (single or double), the javascript dies completely. I need to escape the quotes while maintaining the ability to check if the content matches.
I've tried the escape() and replace() functions (singly and together), but nothing is working.
Example javascript:
function change_question(){
var feedback = document.getElementById('feedback').value; //this is what the user has entered on the page
var stored_feedback = "#trim(StoredFeedback)#"; //this is what is stored in the database; retrieved via ColdFusion
if (feedback != stored_feedback) {
if (confirm('You have unsaved data on the page. Do you wish to discard your changes?')) {
//go to next page
}
}
else {
//go to next page
}
}
Thanks.
The built in jsStringFormat will escape for JavaScript
var stored_feedback = "#jsStringFormat(StoredFeedback)#";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With