简体   繁体   English

从PHP生成的Javascript中转义撇号

[英]Escaping Apostrophes in Javascript Generated From PHP

I am trying to have a textfield with an initial value, where if you click on it, the text disappears, and if you click out before anything has been entered the initial value returns much like the search box on Yahoo answers and many other sites. 我正在尝试使用一个带有初始值的文本字段,如果单击它,文本将消失,并且如果在输入任何内容之前单击退出,则初始值将返回,就像Yahoo答案和许多其他站点上的搜索框一样。

echo "<input type=\"text\" 
onblur=\"if (this.value == '') {this.value = '$cleaned';}\" 
onfocus=\"if (this.value == '$cleaned') {this.value = '';}\" 
value=\"$cleaned\" />\n";

I have a php variable $cleaned as my initial value. 我有一个PHP变量$cleaned作为我的初始值。 This code works, except for the case when the variable cleaned has an apostrophe in it, like $cleaned = "FRIEND'S" . 该代码有效,除了其中cleaned变量中带有撇号的情况(例如$cleaned = "FRIEND'S" Then the code would read this.value = 'FRIEND'S' and the apostrophe in FRIEND'S ends the string early. 然后代码会读this.value = 'FRIEND'S'和在撇号FRIEND'S早结束的字符串。

I have tried using html entities, escaping the apostrophe, and using escaped quotes and cannot get this case to work. 我尝试使用html实体,转义撇号和使用转义引号,但无法使这种情况起作用。 The only solution I have so far is to replace apostrophes with characters that look like apostrophes. 到目前为止,我唯一的解决方案是用看起来像撇号的字符替换撇号。

Does anyone know how to handle this case? 有谁知道如何处理这种情况?

Use json_encode to encode the data to be used in JavaScript and htmlspecialchars to encode the string to be used in a HTML attribute value: 使用json_encode编码要在JavaScript中使用的数据,并使用htmlspecialchars编码要在HTML属性值中使用的字符串:

echo '<input type="text"
onblur="'.htmlspecialchars("if (this.value == '') {this.value = ".json_encode($cleaned).";}").'" 
onfocus="'.htmlspecialchars("if (this.value == ".json_encode($cleaned).") {this.value = '';}").'"
value="'.htmlspecialchars($cleaned).' />'."\n";

But using defaultValue is certainly easier: 但是使用defaultValue当然更容易:

echo '<input type="text"
onblur="'.htmlspecialchars("if (this.value == '') {this.value = defaultValue;}").'" 
onfocus="'.htmlspecialchars("if (this.value == defaultValue) {this.value = '';}").'"
value="'.htmlspecialchars($cleaned).'" />'."\n";

First generate the pure JavaScript code. 首先生成纯JavaScript代码。 You can create valid JavaScript values by using json_encode , like this: 您可以使用json_encode创建有效的JavaScript值,如下所示:

$js = 'if (this.value == '.json_encode($cleaned).') {this.value = "";}';

Then generate the HTML. 然后生成HTML。 You can encode all HTML attributes, including those that contain inline JavaScript code, using htmlspecialchars , like this: 您可以使用htmlspecialchars编码所有HTML属性,包括那些包含内联JavaScript代码的属性,如下所示:

echo '<input type="text" onblur="'.htmlspecialchars($js).'" />';

How about this 这个怎么样

echo '<input type="text" 
onblur="if (this.value == "") {this.value = "' . $cleaned . '";}" 
onfocus="if (this.value == "' . $cleaned . '") {this.value = "";}" 
value="' . $cleaned . '" />\n';

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

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