kb.
kb.

Reputation: 1060

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

Upvotes: 1

Views: 5611

Answers (3)

Robin Knaapen
Robin Knaapen

Reputation: 606

i use regex "someHtml".replace(/\n\s+|\n/g, ""). It's not perfect but it will keep the content intact an delete most of the unnecessary white spaces.

var dom = document.documentElement.outerHTML;
$("body").text(dom.replace(/\n\s+|\n/g, ""));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  lorem ipsum
</div>



<div>
  more text
</div>   <div>
  test 123
</div>

Upvotes: 3

DividedByZero
DividedByZero

Reputation: 4391

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>

Upvotes: 3

Deepak
Deepak

Reputation: 112

You can use jquery for this Code:

  str = str.replace(/\s+/g, '');

Upvotes: 0

Related Questions