I came across this thread because I was looking for a solution to eliminate spaces around divs caused by spaces in the HTML source or linear feeds in my case.
Before I realized that a space could cause these spaces, I was going to go crazy trying to get rid of them. I want to keep the HTML source code for reading, so code compression is not a good solution for me. Even if I handled it this way, it does not commit the divs that are generated by Google and other providers.
I started by creating the following function and calling it on the onload body.
function Compress_Html() { //Remove whitespace between html tags to prevent gaps between divs. document.body.innerHTML = document.body.innerHTML.replace( /(^|>)\s+|\s+(?=<|$)/g, "$1" ); }
This seemed to work just fine, but unfortunately it breaks the Google search box that I have in the footer.
After several attempts to modify the regular expression pattern, I found this regular expression tag http://www.regexpal.com/ . As far as I can tell, the following template does what I need.
( /(^|>)[ \n\t]+/g, ">" )
However, the function still violated the search box. So I moved it to the jQuery ready function. Now it works and does not break the search box.
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script> <script> $( document ).ready(function() { document.body.innerHTML = document.body.innerHTML.replace( /(^|>)[ \n\t]+/g, ">" ); }); </script>
Mfm
source share