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

JavaScriptJquery HTML Export DOM StructurePage to HTML File or Text - Stack Overflow

programmeradmin1浏览0评论

I want to modify through the dom various element properties and then save that page as an html file. View source doesn't always reflect dom adjustments. Is there a way to write the whole page to a file or otherwise get the updated source page into a file?

I want to modify through the dom various element properties and then save that page as an html file. View source doesn't always reflect dom adjustments. Is there a way to write the whole page to a file or otherwise get the updated source page into a file?

Share Improve this question asked Jun 21, 2011 at 15:09 Matt RalstonMatt Ralston 6861 gold badge6 silver badges15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9

I'm thinking this should do the trick for ya.

$('html').html();

JavaScript can't write files when being ran from a browser (security). But you can send this to a PHP script and write it to a file from there. For example:

$.post('write.php', { dom : $('html').html() });

write.php

file_put_contents('new.html', urldecode($_POST['dom']));

In newish browsers (IE 10, FF 20, Chrome 26, Safari 6, Opera 15), you can create a Blob and save it into a file using window.URL.createObjectURL.

Demo, Reference:

objectURL = window.URL.createObjectURL(blob);
  • Where blob is a File object or a Blob object to create a URL object for.
  • objectURL is the generated object URL. The entire contents of the specified file are represented by the text of the URL. It can then be used in window.open.

Example of a Blob:

var parts = ["<p class=\"paragraph\"><a id=\"link\">hey!<\/a><\/p>"];
new Blob(parts, { "type" : "text\/html" });

To list current Blobs in Chrome execute the following in your address bar:

chrome://blob-internals
发布评论

评论列表(0)

  1. 暂无评论