admin管理员组文章数量:1419168
I need to turn an entire HTML document into one, valid JavaScript string.
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>
needs to bee this:
var htmlString="<html><!--here is my html --><div id=\"Main\">some content</div><html>"
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 remend a good approach?
Similar Questions I've Read
Reading entire html file to String?
convert html to javascript
I need to turn an entire HTML document into one, valid JavaScript string.
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>
needs to bee this:
var htmlString="<html><!--here is my html --><div id=\"Main\">some content</div><html>"
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 remend a good approach?
Similar Questions I've Read
Reading entire html file to String?
convert html to javascript
- What language are you using to do this conversion? – Niet the Dark Absol Commented Mar 13, 2014 at 11:43
- I'm familiar with Java, but open to suggestions. Dev environment is OSX. Whatever the most expedient process would be to convert one file (html) to another (js) where the JS file contains only var stringName="<escaped html>" – Paul Commented Mar 13, 2014 at 11:45
- 1 Btw., the string you have shown is not an identical representation of the document shown, because you left out white space (and this can be significant). – C3roe Commented Mar 13, 2014 at 11:48
- Your questions says to use java, but not tagged with 'java' do you mean javascript or java ? - If 'java' tag as java – Rob Sedgwick Commented Mar 13, 2014 at 11:48
- And the easiest way would probably be not to bother with writing any “parsers” or such at all – but just take the whole document content you read and encode it as JSON. – C3roe Commented Mar 13, 2014 at 11:49
2 Answers
Reset to default 4Try this live, jsFiddle
JS
var htmlString = document.getElementsByTagName('html')[0].innerHTML;
console.log(htmlString);
HTML
<p>Hello</p>
<div>Hi div</div>
How about a jQuery GET request?
e.g.
$.get( "http://www.domain./some/page.html", function( data ) {
// data == entire page in html string
});
It will work on php script also (resulting html output that is).
Alternatively, if you want to achieve this PHP, something like this will also work:
<?php
$html_str = file_get_contents('http://www.domain./some/page.html');
?>
本文标签: javaConvert an Entire HTML Page to a Single JavaScript StringStack Overflow
版权声明:本文标题:java - Convert an Entire HTML Page to a Single JavaScript String - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745305903a2652642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论