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

javascript - How to create a JSoup Document including JS? - Stack Overflow

programmeradmin1浏览0评论

I create a JSoup Document which includes two <script type="application/javascript">/* .. */</script> elements.

The Issue: When I call .html() or .toString() JSoup will escape my JavaScript.

if (foo && bar) 

gets

if (foo &amp;&amp; bar)

Is it possible to configure JSoup to ignore <script> Elements while escaping??


This is (basically) how I create my jsoup document.

final Document doc = Document.createShell("");
final Element head = doc.head();
head.appendElement("meta").attr("charset", "utf-8");
final String myJS = ...;
head.appendElement("script").attr("type", "application/javascript").text(myJS);

My current workaround is to replace a placeholder with String.replace on .html(). But this is kinda hacky.

head.appendElement("script").attr("type", "application/javascript").text("$MYJS$");
String s = doc.html();
s = s.replace("$MYJS$", myJS);

I create a JSoup Document which includes two <script type="application/javascript">/* .. */</script> elements.

The Issue: When I call .html() or .toString() JSoup will escape my JavaScript.

if (foo && bar) 

gets

if (foo &amp;&amp; bar)

Is it possible to configure JSoup to ignore <script> Elements while escaping??


This is (basically) how I create my jsoup document.

final Document doc = Document.createShell("");
final Element head = doc.head();
head.appendElement("meta").attr("charset", "utf-8");
final String myJS = ...;
head.appendElement("script").attr("type", "application/javascript").text(myJS);

My current workaround is to replace a placeholder with String.replace on .html(). But this is kinda hacky.

head.appendElement("script").attr("type", "application/javascript").text("$MYJS$");
String s = doc.html();
s = s.replace("$MYJS$", myJS);
Share Improve this question edited Aug 3, 2015 at 15:13 boop asked Aug 3, 2015 at 14:29 boopboop 7,80715 gold badges59 silver badges100 bronze badges 1
  • I've updated my answer. – Alkis Kalogeris Commented Aug 7, 2015 at 14:47
Add a ment  | 

1 Answer 1

Reset to default 7

The outerHtmlHead method can no longer be overridden.

This is what I use:

head
    .appendElement("script")
    .attr("type","text/javascript")
    .appendChild(new DataNode(getJavaScript(),""));
发布评论

评论列表(0)

  1. 暂无评论