Google has the very nice JavaScript compressor called "Closure"
http://closure-compiler.appspot.com/home
But it's a pain to开发者_如何学JAVA use for inline JavaScript within an HTML file.
Question: Does an online tool exist where I simply give the input "uncompress.html" and it spits out the compressed version of that HTML with all inline JavaScript compressed as well?
Minifiers,obfuscators and compressors have been designed to solve the issue of downloading external elements when loading a page. They were never meant to be used for inline JavaScript or CSS since lots of that is normally kept outside of the page in a separate file.
Since you should be gzipping as much as you can, for browsers that can handle gzip, it shouldn't in reality matter that your inline css/javascript isn't minified on the page.
If you use PHP, you might call the compiler of google in this way, and then use the header to return javascript
<?php
$script = file_get_contents('http://www.domain.com/scripts/script.js');
$ch = curl_init('http://closure-compiler.appspot.com/compile');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($script));
$output = curl_exec($ch);
curl_close($ch);
header('Content-type: application/javascript');
echo $output;
?>
link php code
Mirroring AutomatedTester's comment, you really shouldn't put so much inline JavaScript into your HTML file.
But if you really really want to do that for some reason, why can't you:
- Make a text file containing the JavaScript
- Compress it with Closure
- Copy it into your
<script></script>
tag
I know this is not automatic, but you should only need to do this when you are deploying, and you are not deploying every hour, right?
I haven't seen much need to minimize HTML, since gzip typically does a good enough job...
精彩评论