最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

uri - "save" current page state using javascript - Stack Overflow

programmeradmin1浏览0评论

What I'm trying to do is have a function create a uri anchor to redraw/rerender/(call it what you want) the entire page

Basically I want to be able to convert any page into a URI scheme so that when I navigate to such a link I get the entire page as is, kinda like saving a webpage. For example if I were to be editing a page and wanted to resume later with all the textareas just the way they are and the forms filled out, or if I wanted to save someones (small) page without having to worry that his site will go down and without having to save files on my puter (I want to use bookmarklets)

Here's what I have so far:

html = '<html>' + document.documentElement.innerHTML + '</html>';
//html = html.replace(/"/g, '\\"');
a = document.createElement('a');
a.href = 'data:text/html;charset=utf-8,' + html;
a.innerHTML = 'click here';
document.body.appendChild(a);

You see what I'm trying to do. Ok now the hard part is somehow using a regex to replace all double quotes that are already in double quotes but not ones that aren't.

For example if we create the page

<html><body>Testing</body></html>

and run the function enough times we're gonna get some issues with the 3rd and on links.

See what I mean: /

What I'm trying to do is have a function create a uri anchor to redraw/rerender/(call it what you want) the entire page

Basically I want to be able to convert any page into a URI scheme so that when I navigate to such a link I get the entire page as is, kinda like saving a webpage. For example if I were to be editing a page and wanted to resume later with all the textareas just the way they are and the forms filled out, or if I wanted to save someones (small) page without having to worry that his site will go down and without having to save files on my puter (I want to use bookmarklets)

Here's what I have so far:

html = '<html>' + document.documentElement.innerHTML + '</html>';
//html = html.replace(/"/g, '\\"');
a = document.createElement('a');
a.href = 'data:text/html;charset=utf-8,' + html;
a.innerHTML = 'click here';
document.body.appendChild(a);

You see what I'm trying to do. Ok now the hard part is somehow using a regex to replace all double quotes that are already in double quotes but not ones that aren't.

For example if we create the page

<html><body>Testing</body></html>

and run the function enough times we're gonna get some issues with the 3rd and on links.

See what I mean: http://jsfiddle/AvSh3/3/

Share Improve this question edited Dec 19, 2010 at 4:33 Brad Mace 27.9k18 gold badges109 silver badges152 bronze badges asked Dec 19, 2010 at 2:41 qwertymkqwertymk 35.4k30 gold badges124 silver badges184 bronze badges 3
  • I modified your question so hopefully more people who can help will find it/understand it – Brad Mace Commented Dec 19, 2010 at 3:09
  • I can't wait to see what es of this. I really don't fully understand what the goal is, but something tells me the results will be cool. – Hemlock Commented Dec 19, 2010 at 21:54
  • Do you want to save all of the JavaScript variables on the page as well as the HTML elements? – Anderson Green Commented Mar 18, 2013 at 18:49
Add a ment  | 

3 Answers 3

Reset to default 3

Use the built-in escape() function:

html = escape(html);

I've reworked it into

var html = '<html>' + $("html").html() + '</html>';
$('<a></a>').html("click here")
.attr("href", 'data:text/html;charset=utf-8,' + escape(html))
.appendTo($("body"));

Which doesn't display correctly, but when viewing source everything looks correct. Maybe some other special parameter is required?

This works when testing on my own page:

a = document.createElement('a');
a.href = 'data:text/html;charset=utf-8,<html>' +
    escape(document.documentElement.innerHTML) + '</html>';
a.innerHTML = 'click here';
document.body.appendChild(a);

I'm guessing that it's just a jsBin/jsFiddle technicality but I have no clue why. Anyway if people want to use this to make bookmarklets heres the link:

....

Well I can't figure out how to make a bookmarklet link in SO, if you want just create a new bookmark with this location:

javascript:a=document.createElement("a");a.href="data:text/html;charset=utf-8,<html>"+escape(document.documentElement.innerHTML)+"</html>";a.innerHTML="click here";document.body.appendChild(a);

Anyway with this fun tool we can do things like Jon does in the first link here:

http://wundes./bookmarklets.html

发布评论

评论列表(0)

  1. 暂无评论