将整个 HTML 页面转换为单个 JavaScript 字符串 [英] Convert an Entire HTML Page to a Single JavaScript String

查看:27
本文介绍了将整个 HTML 页面转换为单个 JavaScript 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要将整个 HTML 文档转换为一个有效的 JavaScript 字符串.

这是一个需要定期完成的过程.例如:


这个:

This is a process that will need to be done regularly. For example:


This:

<html>
    <!--here is my html-->
    <div id="Main">some content</div>
    </html>

需要变成这样:

var htmlString="<html><!--here is my html --><div id=\"Main\">some content</div><html>"

<小时>

根据我的准备,我最初的想法是用 Java 读取文件的内容并编写我自己的解析器.我不认为使用 shell 脚本或类似的方法可以做到这一点?(我在 OSX 上)


Based on what I've ready my initial thought is to read the contents of the file with Java and write my own parser. I don't think this would be possible with a shell script or the like? (I'm on OSX)

有人可以推荐一个好的方法吗?

Can someone recommend a good approach?

我读过的类似问题
将整个 html 文件读取为字符串?
将 html 转换为 javascript

推荐答案

试试这个,jsFiddle

JS

var htmlString = document.getElementsByTagName('html')[0].innerHTML;
console.log(htmlString);

HTML

<p>Hello</p>
<div>Hi div</div>

这篇关于将整个 HTML 页面转换为单个 JavaScript 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆