admin管理员组文章数量:1315336
After some Google search, I did not find anything fill my need. I want to save the current web page just as what it is. I mean, many web pages has Javascript executed and CSS changed, so after some user interactive, the web page may be different from the one when it is firstly loaded into browser. And I want to save the current web page state to the sever and rendering it in the server. Is there any Javascript library for this task? Thanks!
After some Google search, I did not find anything fill my need. I want to save the current web page just as what it is. I mean, many web pages has Javascript executed and CSS changed, so after some user interactive, the web page may be different from the one when it is firstly loaded into browser. And I want to save the current web page state to the sever and rendering it in the server. Is there any Javascript library for this task? Thanks!
Share Improve this question asked Jan 9, 2010 at 6:33 Yang BoYang Bo 6692 gold badges8 silver badges20 bronze badges4 Answers
Reset to default 2Even simpler:
var serialized = document.documentElement.innerHTML
outerHTML
instead of innerHTML
would be better, but it doesn't work in Firefox.
Let's test it.
>>> document.body.style.color = 'red'; >>> document.documentElement.innerHTML ... <body style="color: red;"> ...
I'm working on something rather similar and wanted to share a summary of what I'm noticing with the innerHTML in IE8, FF3.6, and CHROME 5.0
IE
- Strips the quotes from around many of the element attributes
- Singleton nodes aren't self closed
- If the values on the elements change after the HTML has been loaded, it picks up the new values
FF, CHROME
- Singleton nodes aren't self closed
- If the values on the elements change after the HTML has been loaded, it does NOT pick up the new values. It only picks up the default values set in the HTML upon initial rendering.
Serializing a plete web page is as simple as:
var serialized = document.body.innerHTML;
If you really need the full document, including the head, then:
var serialized =
'<head>' +
document.getElementsByTagName('head')[0].innerHTML +
'</head><body>' +
document.body.innerHTML +
'</body>';
Now all you need to do is submit it via AJAX.
About server side rendering, it depends what you mean by rendering. I'm currently using wkhtmltopdf to implement a 'save as pdf' feature on my site. It uses webKit to render the HTML prior to generating the PDF so it fully supports CSS and javascript.
And if you need to save it to an image instead of a PDF file you can always use ghostscript to print the PDF to a JPG/PNG file.
this post mentions a serialiseWithStyles() function, the function calculates styles for each element, and print the styles inline. this eliminate need for separate stylesheets.
then to submit it to a server, send a post request. use ajax or a plain form.
本文标签: javascriptSerialize HTMLDocument and then rendering it in the serverStack Overflow
版权声明:本文标题:javascript - Serialize HTMLDocument and then rendering it in the server? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741976899a2408176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论