简体   繁体   中英

Is it possible to compress/decompress my html code with jquery?

I've searched everything about something like "compress html javascript" in the Web, but found nothing to answer my question, but I cannot understand what's wrong. The problem: if you have something like <span>blah<span>blah</span>blah<span>blah</span></span> in your html page, why don't you try to compress this? For example,

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<style>
.click {color: blue; cursor: pointer;}
.encoded {display:none;}
</style>
<script src="jquery.js"></script>
<script>
$(document).ready(function () {
$.fn.encode = function () {
//encoding code
};
$.fn.decode = function () {
//decoding code
};
$('.click').click(function () {
var decoded = $(this).next('.encoded').text().decode();
$(this).next('.encoded').removeClass('encoded').addClass('decoded').html(decoded);
});
});
</script>
</head>
<body>
<div class="click">click</div>
<div class="encoded">AbCxYz*_()+=…
<!-- decoded html: <span>blah<span>blah</span>blah<span>blah</span></span>-->
</div>
</body>
</html>

Is there any functions that may be used for those encode/decode parts? Of course, base64 is not a solution, because that html must be compressed at first , and then something like base64 must be applied to the result...

The support for gzip compression available in just about any browser and web server is much more efficient than the javascript transfer. In fact, with many web servers you can upload your web pages as .html.gz (for most browsers) and .html (for clients that do not support compression; mostly bots, no real users) and the web browser will automatically serve the compressed version when supported.

Note that in particular more clients support the HTTP (not limited to HTML - works for CSS as well) compression than javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM