kb. kb. - 5 months ago 39
Javascript Question

Use JQuery to minify HTML

I am using asppdf to create a PDF from HTML.

It looks like your HTML needs to be in a single line with all whitespace removed, before its passed to the ImportFromUrl method, this is an example from the support site:

str = "<HTML><TABLE><TR><TD>Text1</TD><TD>Text2</TD></TR></TABLE></HTML>"
Doc.ImportFromUrl str


Currently my HTML is pulled in from an external page & it's all formatted, so i need it to be like the above example. Can I use jQuery to do this?

Reference http://www.asppdf.com/manual_13.html#13_5

Answer

Use this regular expression for spaces only:

var HTML = "<h1>hh ee</h1>     <h2>heyy  heyyy</h2>";
document.getElementById("after").innerText = HTML.replace(/>[ ]+</g, "><");
document.getElementById("before").innerText = HTML;
<h1 id="before"></h1>
Becomes
<h1 id="after"></h1>
And this for tabs, new lines and spaces:

var HTML = "<h1>hh ee</h1>    <h2>heyy  heyyy</h2>";
document.getElementById("after").innerText = HTML.replace(/>[\n\t ]+</g, "><");
document.getElementById("before").innerText = HTML;
<h1 id="before"></h1>
Becomes
<h1 id="after"></h1>

Comments