In a variable, string data is coming from text input like following.
在变量中,字符串数据来自文本输入,如下所示。
var noteStr="<?php echo $_REQUEST["noteStr"];?>";
If user given any double quotes in text input then javascript give an error as expected. I am not finding a way to use javascript str.replace() in this scenario because before using that on string variable javascript engine generate errors.
如果用户在文本输入中给出任何双引号,则javascript会按预期给出错误。我没有找到在这种情况下使用javascript str.replace()的方法,因为在使用字符串变量之前,javascript引擎会生成错误。
How can i solve this problem using javascript? Advance thanks for help.
如何使用javascript解决这个问题?谢谢你的帮助。
2 个解决方案
#1
Use PHP function htmlspecialchars().
使用PHP函数htmlspecialchars()。
var noteStr="<?php echo htmlspecialchars($_REQUEST["noteStr"]); ?>";
And here's JavaScript equivalent:
这里是JavaScript的等价物:
function escapeHtml(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
Source: HtmlSpecialChars equivalent in Javascript?
来源:HtmlSpecialChars相当于Javascript?
#2
There is an awesome inbuild function in Javascript. escape() funtion.
在Javascript中有一个很棒的inbuild函数。 escape()函数。
<script>
document.write(escape("Hello friends? Here is the solution!"));
</script>
Will become-
Hello%20friends%3F%20Here%20is%20the%20solution%21
Check out- http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_escape for reference
查看http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_escape以供参考
This can also be achieved by using Prototype in Javascript. A simple example-
这也可以通过在Javascript中使用Prototype来实现。一个简单的例子 -
var str = '<div class="article">This is an article</div>';
document.write( str.escapeHTML() );
Will become-
<div class="article">This is an article</div>
Source : http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=prototype_83
资料来源:http://www.tutorialspoint.com/cgi-bin/practice.cgi?file = prototype_83
Hope it helps! :)
希望能帮助到你! :)
#1
Use PHP function htmlspecialchars().
使用PHP函数htmlspecialchars()。
var noteStr="<?php echo htmlspecialchars($_REQUEST["noteStr"]); ?>";
And here's JavaScript equivalent:
这里是JavaScript的等价物:
function escapeHtml(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
Source: HtmlSpecialChars equivalent in Javascript?
来源:HtmlSpecialChars相当于Javascript?
#2
There is an awesome inbuild function in Javascript. escape() funtion.
在Javascript中有一个很棒的inbuild函数。 escape()函数。
<script>
document.write(escape("Hello friends? Here is the solution!"));
</script>
Will become-
Hello%20friends%3F%20Here%20is%20the%20solution%21
Check out- http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_escape for reference
查看http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_escape以供参考
This can also be achieved by using Prototype in Javascript. A simple example-
这也可以通过在Javascript中使用Prototype来实现。一个简单的例子 -
var str = '<div class="article">This is an article</div>';
document.write( str.escapeHTML() );
Will become-
<div class="article">This is an article</div>
Source : http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=prototype_83
资料来源:http://www.tutorialspoint.com/cgi-bin/practice.cgi?file = prototype_83
Hope it helps! :)
希望能帮助到你! :)