Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert html node to one line string(minify)

I have a dom node in variable and i want to remove all enter/line break, tabs between the html tags. Basically i want to minify it without using external library. How can i do it.

var target = document.getElementById('myid');
var wrap = document.createElement('div');
wrap.appendChild(target.cloneNode(true));

wrap contains the node..

like image 810
ghost... Avatar asked Mar 19 '14 06:03

ghost...


People also ask

How do I make one line in HTML code?

In this article, we will add a single line break using the <br> tag. It s used to give the single line break. It is the empty tag so it does not contain end tag.

How do you minify HTML code?

Go to minifycode.com and click the CSS minifier tab. Then paste the CSS code into the input box and click the Minify CSS button. After the new minified code is generated, copy the code. Then go back to the css file of your website and replace the code with the new minified version.

How do you make a single line in JavaScript?

Single line comments start with // . Any text between // and the end of the line will be ignored by JavaScript (will not be executed).


2 Answers

Not elegant, but should work

target.innerHTML = target.innerHTML.replace(/\n|\t/g, ' ');
like image 174
sabof Avatar answered Sep 19 '22 17:09

sabof


You could replace the line breaks with an empty string target.replace(/(\r\n|\n|\r)/gm,"");

like image 41
Simo D'lo Mafuxwana Avatar answered Sep 17 '22 17:09

Simo D'lo Mafuxwana