1

I'm looking for a way to minify my HTML code that's retrieved with Javascript like this

document.getElementById("content").innerHTML;

The HTML is very disorganized and poorly indented but its w3c validated. I came across this plugin https://github.com/kangax/html-minifier But need help implementing with raw JS/jQuery because it only provides instructions for node.js, and my app isn't server-based.

1 Answer 1

1

Include the file from dist directory in your page and use minify function. For reference see the options and the demo they provide.

Below I implemented it into a quick demo that removes the attribute value quotes:

$("#input").on("input", function() {
  $("#output").val(minify($(this).val(), {
    removeAttributeQuotes: true,
    collapseWhitespace: true
      // other options
  }));
});
<script src="https://rawgit.com/kangax/html-minifier/50fad6e3a7a77f37671afae6782f557bd686bc8b/dist/htmlminifier.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Input</h1>
<textarea id="input"></textarea>

<h1>Output</h1>
<textarea id="output"></textarea>

Sign up to request clarification or add additional context in comments.

2 Comments

The demo you provided isn't actually minifying the code.
@Shabbb Supposing you define minifying collapsing whitespaces, add collapseWhitespace: true option. See edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.