简体   繁体   English

ColdFusion / Javascript Escape单引号

[英]ColdFusion/Javascript Escape Single Quote

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. 我有一个ColdFusion应用程序,用户可以在多个屏幕上输入文本。 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. 我有一个javascript函数,它根据存储在数据库中的内容检查输入的文本,并弹出一个确认窗口,询问他们是否要保存/放弃更改。

If the user-entered text contains quotes (single or double), the javascript dies completely. 如果用户输入的文本包含引号(单引号或双引号),则javascript完全死亡。 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. 我已经尝试了escape()和replace()函数(单独和一起),但没有任何工作。

Example javascript: 示例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. 谢谢。

内置的jsStringFormat将为JavaScript转义

var stored_feedback = "#jsStringFormat(StoredFeedback)#";

I don't know ColdFusion but according to the docs: 我不知道ColdFusion但根据文档:

var stored_feedback = #SerializeJSON( trim(StoredFeedback) )#; 

I think you shouldn't need to add quotes explicitly, as a string when serialized to JSON should end up with double quotes anyway. 认为您不应该显式添加引号,因为序列化为JSON时的字符串最终应该以双引号结束。 Again, I can't test this myself. 再说一遍,我不能自己测试一下。

If you are looking for a ColdFusion solution, you'll probably want to use this: 如果您正在寻找ColdFusion解决方案,您可能希望使用此方法:

HTMLEditFormat(string)

HTML-escaped string string. HTML转义字符串字符串。 Return characters are removed; 返回字符已删除; line feed characters are preserved. 换行符保留。 Characters with special meanings in HTML are converted to HTML character entities such as >. HTML中具有特殊含义的字符将转换为HTML字符实体,例如>。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM