Not elegant, but should work

target.innerHTML = target.innerHTML.replace(/\n|\t/g, ' ');
Answer from sabof on Stack Overflow
🌐
Kangax
kangax.github.io › html-minifier
HTML minifier
Process scripts Comma-delimited string corresponding to types of script elements to process through minifier (e.g. text/ng-template, text/x-handlebars-template) Quote character Type of quote to use for attribute values (' or ") Remove attribute quotes Remove quotes around attributes when possible ... Remove redundant attributes Remove attributes when value matches default. Remove script type attributes Remove type="text/javascript" from script tags.
🌐
GitHub
github.com › ianstormtaylor › minify
GitHub - ianstormtaylor/minify: Simple, clean API for minifying Javascript, HTML or CSS.
var minify = require('minify'); // choose javascript, html or css var js = minify.js('js string'); var html = minify.html('html string'); var css = minify.css('css string'); // or pass an unknown string var min = minify('unknown string');
Starred by 110 users
Forked by 13 users
Languages   JavaScript 100.0% | JavaScript 100.0%
🌐
GitHub
github.com › kangax › html-minifier
GitHub - kangax/html-minifier: Javascript-based HTML compressor/minifier (with Node.js support) · GitHub
Javascript-based HTML compressor/minifier (with Node.js support) - kangax/html-minifier
Starred by 5.1K users
Forked by 583 users
Languages   JavaScript 94.9% | HTML 4.6% | CSS 0.5%
🌐
npm
npmjs.com › package › minify-html-literals
minify-html-literals - npm
Minify HTML markup inside JavaScript template literal strings.
      » npm install minify-html-literals
    
Published   Mar 09, 2021
Version   1.3.5
Author   Liz Mitchell
🌐
GitHub
github.com › asyncLiz › minify-html-literals
GitHub - asyncliz/minify-html-literals: Minify HTML template literal strings
Minify HTML markup inside JavaScript template literal strings.
Starred by 70 users
Forked by 13 users
Languages   TypeScript 100.0% | TypeScript 100.0%
🌐
npm
npmjs.com › package › html-minifier
html-minifier - npm
Highly configurable, well-tested, JavaScript-based HTML minifier.. Latest version: 4.0.0, last published: 7 years ago. Start using html-minifier in your project by running `npm i html-minifier`. There are 2177 other projects in the npm registry using html-minifier.
      » npm install html-minifier
    
Published   Apr 01, 2019
Version   4.0.0
Author   Juriy "kangax" Zaytsev
🌐
DEV Community
dev.to › stackfindover › how-to-create-code-compressor-in-javascript-html-minifier-32i
How to create code compressor in JavaScript | HTML Minifier - DEV Community
June 29, 2021 - @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300&display=swap'); * { padding: 0; margin: 0; font-family: 'Poppins', sans-serif; } body { display: flex; align-items: center; justify-content: center; height: 100vh; width: 100vw; overflow: hidden; } .codeminify { display: grid; grid-template-columns: auto auto auto; } textarea { padding: 10px; min-width: 420px; min-height: 300px; font-size: 16px; } button#htmlMinify { display: block; width: 150px; height: 40px; font-size: 16px; font-weight: 600; background: #4b00ff; color: #fff; border: transparent; cursor: pointer; outline: 0; margin: 0 10px; } In this step, we will add some JavaScript code to minify html code.
Find elsewhere
🌐
Code Beautify
codebeautify.org › text-minifier
Minify Text - CodeBeautify
HTML Decode · Base64-Encode · Base64-Decode · URL-Encode A String · URL-Decode A String · String to Hex Converter · Hex to String Converter · String to Binary Converter · Binary to String Converter · Case Converter · Delimited Text Extractor · Remove Accents · Remove Duplicate Lines · Remove Empty Lines · Remove Extra Spaces · Remove Line Breaks · Remove Lines Containing · Sort Text Lines · CSS Validator · JAVASCRIPT Validator ·
🌐
GitHub
gist.github.com › yairEO › 9d8e18a330aab275bc86b56075fc234e
HTML string minify / compress to single-line · GitHub
HTML string minify / compress to single-line · Raw · minifyHTML.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
GitHub
github.com › wilsonzlin › minify-html
GitHub - wilsonzlin/minify-html: Extremely fast and smart HTML + JS + CSS minifier, available for Rust, Deno, Java, Node.js, Python, Ruby, and WASM · GitHub
Extremely fast and smart HTML + JS + CSS minifier, available for Rust, Deno, Java, Node.js, Python, Ruby, and WASM - wilsonzlin/minify-html
Starred by 1.1K users
Forked by 60 users
Languages   Rust 86.8% | JavaScript 7.9% | Java 2.9% | Shell 1.5%
🌐
Code Beautify
codebeautify.org › minify-js
Minify JS is JavaScript Minifier online
This tool allows loading the Javascript URL to minify. Use your JS URL to compress. Click on the URL button, Enter URL and Submit. Users can also minify JS File by uploading the file. Minify JS Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari. ... JS data Try it. var carInsuranceCompany = { name: "Geico", market_capital: "$34.9 billion", }; var carInsuranceCompanyObj = JSON.stringify(obj); document.getElementById("insurance").innerHTML = carInsuranceCompanyObj;
🌐
Minifier
minifier.org › html-minifier
HTML Minifier / Compressor - Minify HTML code online
Process scripts Comma-delimited string corresponding to types of script elements to process through minifier (e.g. text/ng-template, text/x-handlebars-template) Quote character Type of quote to use for attribute values (' or ") Remove attribute quotes Remove quotes around attributes when possible ... Remove redundant attributes Remove attributes when value matches default. Remove script type attributes Remove type="text/javascript" from script tags.
🌐
Minifier
minifier.org
Minify JS / CSS - JavaScript and CSS Minifier / Compressor
You can paste .js or .css file, or just plain javascript or CSS code. This JS and CSS minifier removes whitespace, strips comments, combines files, and optimizes/shortens a few common programming patterns.
🌐
Toptal
toptal.com › developers › javascript-minifier
JavaScript Minifier & Compressor | Toptal®
Use our JavaScript Minifier & Compressor tool to reduce JavaScript code size and make your website load faster. Get started for free now.
Top answer
1 of 9
9

Edit 2 (Feb 22, 2017): Now the best tool to minify your assets (and a whole lot more, by adding loaders and plugins) is definitely Webpack.

Example of config to move all your .css in one file and minify it:

{
  test: /\.css$/,
  use: [
    {
      loader: 'css-loader',
      options: {
        minimize: true
      }
    }
  ]
}

Edit 1 (Sept 16, 2014): Even better, now you have task runners like Gulp or Grunt.

Task runners are small applications that are used to automate many of the time consuming, boring (but very important) tasks that you have to do while developing a project. These include tasks such as running tests, concatenating files, minification, and CSS preprocessing. By simply creating a task file, you can instruct the task runner to automatically take care of just about any development task you can think of as you make changes to your files. It’s a very simple idea that will save you a lot of time and allow you to stay focused on development.

Must read: Getting started with Gulp.js

Example of task with JavaScript concatenation and minification (and JSHint):

gulp.task('scripts', function() {
  return gulp.src('src/scripts/**/*.js')
    .pipe(jshint('.jshintrc'))
    .pipe(jshint.reporter('default'))
    .pipe(concat('main.js'))
    .pipe(gulp.dest('dist/assets/js'))
    .pipe(rename({suffix: '.min'}))
    .pipe(uglify())
    .pipe(gulp.dest('dist/assets/js'))
    .pipe(notify({ message: 'Scripts task complete' }));
});

Original answer (Jul 19, 2012): I advise the HTML5 Boilerplate Build Script which can minify your JS and CSS.

2 of 9
7

Good javascript compressor is also Google's Closure Compiler and vice versa, to make compressed code a bit better readible you can use Javascript Beautifier. You can also have a look at phpEasyMin project.

🌐
Jscompress
jscompress.com
JSCompress - The JavaScript Compression Tool
JSCompress is an online JavaScript compressor that allows you to compress and minify all of your JS files by up to 80% of their original size.
🌐
Onlineutility
onlineutility.in › Text_Minify
Minify Text - onlineutility.in
Minify Text Utility is useful in creating HTML, CSS and JavaScript (JS) files. Using this minify online tool one can easily minify css, minify js, minify html, minify jsonminify xml,minify code,minify url etc.