admin 管理员组文章数量: 1086019
var input;
// method 1
input = document.getElementById('address').value;
alert(input)
// method 2
eval('input = "'+document.getElementById('address').value+'"')
alert(input)
method 1 is working fine, but method 2 is not working when newline characters are inputted and it says "unterminated string literal".
I need to store values using eval anyway. So please help me.
var input;
// method 1
input = document.getElementById('address').value;
alert(input)
// method 2
eval('input = "'+document.getElementById('address').value+'"')
alert(input)
method 1 is working fine, but method 2 is not working when newline characters are inputted and it says "unterminated string literal".
I need to store values using eval anyway. So please help me.
Share Improve this question asked Sep 30, 2010 at 11:38 A.N.M. Saiful IslamA.N.M. Saiful Islam 2,1387 gold badges28 silver badges35 bronze badges3 Answers
Reset to default 4Use string.replace(/\r?\n/g, "\\n")
to escape the newlines.
That's because you are actually evaluating code that looks like this, which is ofcourse wrong in javascript
var test = "Lorem ipsum
dolor sit amet";
Why do you need the eval? It looks not really necessary in this case. Maybe we can help you get around the eval?
Try:
const address = document.getElementById('address').value
eval('input = ' + JSON.stringify(String(address)))
String(address) will guarantee that the object is a string. JSON.stringify will return a double quoted string parsable with eval.
本文标签: how to escape special characters within eval using javascriptStack Overflow
版权声明:本文标题:how to escape special characters within eval using javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744018776a2519417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论