我有一些Ajaxing正在进行,我有2块源代码。第一个是:
window.lastSavedContents = "<?php echo htmlentities( $contents ); ?>";
第二种是用户在文本编辑器中键入的内容。问题是window.lastSavedContents
是htmlentities
编码,而第二个块不是。
如何检查等效性?
答案 0 :(得分:1)
首先,请勿使用仅htmlentities
,<
,>
和&
进行编码的"
,htmlspecialchars
。这样,您可以使用
var decoded = encoded.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/"/g, '"')
.replace(/>/g, '>');
如果您仍想使用htmlentities
,则只需创建一个临时div
:
var div = document.createElement('div');
div.innerHTML = encoded;
var decoded = div.firstChild.nodeValue;
答案 1 :(得分:0)
您可以找到绕实体编码数据的方法,也可以在Javascript中解码window.lastSavedContents
。这是一个可以帮助您实现这一目标的功能:http://phpjs.org/functions/html_entity_decode:424