如何在localstorage中缓存DOM元素? [英] how can I cache DOM element in localstorage?

查看:479
本文介绍了如何在localstorage中缓存DOM元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在localstorage中缓存页面的xml DOM,我将它存储在一个数组中并将其转换为json字符串,但我收到此错误:Uncaught TypeError:将循环结构转换为JSON

我该怎么做?





我加载一个xml文件如下:

I want to cache xml DOM of a page in localstorage, I store it in an array and convert it into a json string, but i get this error : "Uncaught TypeError: Converting circular structure to JSON "
how can i do this?


I load an xml file as follow :

	xml = loadXMLDoc(load_link);
 
//...
function loadXMLDoc(dname){
	if (window.ActiveXObject)
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP.3.0");
	else <br>
		xmlhttp=new XMLHttpRequest();
					
		xmlhttp.open("GET",dname,false);
		xmlhttp.send("");
		return xmlhttp.responseXML;
	}
	//...
	postInfo[5] = xml;
	post[id] = postInfo;
	localStorage["blogInfo"] = JSON.stringify(post); // in this line i get an exeption





以上代码来自OP的评论 - Maciej Los

[/ EDIT]



Above code comes from OP's comment - Maciej Los
[/EDIT]

推荐答案

感谢您的澄清。



该例外与 localStorate 无关;你正试图正确使用它。



JSON.strignify 抛出异常。这种类型的序列化不支持带循环引用的数据图(某些技术,例如.NET Data Contract很容易处理这种情况)。您在 post 引用的数据结构中有一些循环引用。您可以查看构成此结构的代码。



-SA
Thank you for the clarification.

The exception is unrelated to localStorate; you are trying to use it correctly.

The exception is thrown by JSON.strignify. This type of serialization does not support data graphs with circular references (some technologies, such as .NET Data Contract handle such cases easily). You have some circular reference in your data structure referenced by post. You can review the code which forms this structure.

—SA


这篇关于如何在localstorage中缓存DOM元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆