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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744018775a4544475.html
评论列表(0条)