简体   繁体   中英

javascript Uncaught ReferenceError: uneval is not defined

Uncaught ReferenceError: uneval is not defined

It works well with Firefox, but not work with Google Chrome.

I try:

var getSaved = [['ABC', 'http://...'], ['DEF', 'http://...'], ['...etc...']];
var get_saved_items = eval(getSaved);
var name = [], url = [];
for (var i = 0; i < get_saved_items.length; i++) {
    name[i].push(get_saved_items[i][0]);
    url[i].push(get_saved_items[i][1]);
};
window.localStorage.setItem('saved_name', uneval(name));
window.localStorage.setItem('saved_links', uneval(url));

it returns: Uncaught ReferenceError: uneval is not defined

What's the problem?

我相信uneval只是在SpiderMonkey中,所以也只是基于Mozilla的浏览器。

Using 'eval' is not recommended because of different reasons (you can google it). uneval will not work in other browsers.

For your requirement you can use a JSON parser. Widely used one is Crockford's JSON parser.

https://github.com/douglascrockford/JSON-js/blob/master/json2.js

var numbers = '[1, 2, 3, 4, 5, 6]'; 
var names = '["abc", "def", "ghi", "jkl", "mno", "pqr"]';

// string to Javascript object 
var numbersObject = JSON.parse(numbers);
var namesObject = JSON.parse(names);

console.log(numbersObject);
console.log(namesObject);

// Javascript object to string
numbers = JSON.stringify(numbersObject);
names = JSON.stringify(namesObject);

console.log(numbers);
console.log(names);

demo : http://jsfiddle.net/xVvBp/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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