“保存”使用 javascript 的当前页面状态
我想做的是让一个函数创建一个 uri 锚点来重绘/重新渲染/(将其称为您想要的)整个页面
基本上我希望能够将任何页面转换为 URI 方案,以便当我导航到这样的链接我可以按原样获取整个页面,有点像保存网页。例如,如果我要编辑一个页面并希望稍后恢复所有文本区域并填写表格,或者如果我想保存某人的(小)页面而不必担心他的网站会消失下来,而无需在我的计算机上保存文件(我想使用书签)
这是到目前为止我所拥有的:
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);
您知道我正在尝试做什么。 好吧,现在最困难的部分是以某种方式使用正则表达式来替换所有已经在双引号中的双引号,但不替换那些不在双引号中的双引号。
例如,如果我们创建页面
<html><body>Testing</body></html>
并运行该函数足够多次,我们就会在第三个和链接上遇到一些问题。
明白我的意思:http://jsfiddle.net/AvSh3/3/
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 computer (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.net/AvSh3/3/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用内置的 escape() 函数:
Use the built-in escape() function:
我已将其重新设计为
“显示不正确”,但是当查看源代码时,一切看起来都是正确的。也许还需要一些其他特殊参数?
I've reworked it into
Which doesn't display correctly, but when viewing source everything looks correct. Maybe some other special parameter is required?
在我自己的页面上测试时,这是有效的:
我猜这只是 jsBin/jsFiddle 技术问题,但我不知道为什么。无论如何,如果人们想用它来制作小书签,请点击以下链接:
...
好吧,我不知道如何在 SO 中制作小书签链接,如果您只想使用此位置创建一个新书签:
无论如何,这很有趣工具,我们可以像 Jon 在第一个链接中所做的那样:
http://wundes.com/bookmarklets.html
This works when testing on my own page:
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:
Anyway with this fun tool we can do things like Jon does in the first link here:
http://wundes.com/bookmarklets.html