开发者

Save the content of <a href> in the localstorage

I want to save the content of the <a href> In the localstorage and reuse it. How can i do that? I want to keep开发者_JAVA技巧 the link working. I want to use it to link to webapps (user provided) and i want to achieve that with the localstorage (i am building an online os For personal(and maybe) public use it won't be as big like eyeos or jolicloud).


If you want to save the whole link (including attributes):

<script>
function setLinks(){
     var all_links = document.getElementById("container").innerHTML;
     localStorage.setItem("savedLinkHTML", all_links);
}
function getLinks(){
    var all_links = localStorage.getItem("savedLinkHTML");
    if(all_links) document.getElementById("container").innerHTML = all_links;
}
window.onload = function(){
    getLinks();
}
window.onunload = function(){
    setLinks();
}
</script>
 ...
<div id="savedLinks"></div>

You can create your own functions to dynamically add more links (even images) to the container, which are automatically saved when leaving the page, and shown again when visiting the page.

See also: https://developer.mozilla.org/en/DOM/Storage


Why not just grab the URL

var url = window.location.href;

and then store it in the value for key(i)? I know that sounds too simple, but that is what you are asking... isn't it? You would just need a naming system for retrieval.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜