简体   繁体   English

使用javascript“保存”当前页面状态

[英]“save” current page state using javascript

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 我要做的是有一个函数创建一个uri锚来重绘/重新渲染/(称之为你想要的)整个页面

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. 基本上我希望能够将任何页面转换为URI方案,这样当我导航到这样的链接时,我得到整个页面,就像保存网页一样。 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 computer (I want to use bookmarklets) 例如,如果我要编辑一个页面,并希望稍后恢复所有textareas就像他们的方式和表格填写,或者如果我想保存某些(小)页面而不必担心他的网站将去下来而不必在我的电脑上保存文件(我想使用bookmarklet)

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. 并运行该功能足够多次我们将在第3和链接上遇到一些问题。

See what I mean: http://jsfiddle.net/AvSh3/3/ 看看我的意思: http//jsfiddle.net/AvSh3/3/

使用内置的escape()函数:

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. 我猜这只是一个jsBin / jsFiddle技术性但我不知道为什么。 Anyway if people want to use this to make bookmarklets heres the link: 无论如何,如果人们想使用它来制作bookmarklet,那就是链接:

.... ....

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: 好吧,如果你只想用这个位置创建一个新的书签,我无法弄清楚如何在SO中创建一个bookmarklet链接:

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: 无论如何,通过这个有趣的工具,我们可以在第一个链接中执行Jon所做的事情:

http://wundes.com/bookmarklets.html http://wundes.com/bookmarklets.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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