javascript字符串代码在前端的美化、格式化展示处理
2018-02-07 12:39
691 查看
第一步:在百度网盘上下载两个js文件;
第二步:在自己的jsp或html头部引入该两个js文件:
<script type="text/javascript" src="beautify.js"></script>
<script type="text/javascript" src="beautify-html.js"></script><xmp class="beautify">你需要美化的javascript脚本</xmp>第三步:在自己的js中定义方法如下:var the = { beautify_in_progress: false };
//this dummy function alleviates Chrome large string corruption by probably shoveling the corruption bug to some other area
if (/chrome/.test(navigator.userAgent.toLowerCase())) {
String.prototype.old_charAt = String.prototype.charAt;
String.prototype.charAt = function (n) { return this.old_charAt(n); }
}
function unpacker_filter(source) {
var trailing_comments = '';
var comment = '';
var found = false;
do {
found = false;
if (/^\s*\/\*/.test(source)) {
found = true;
comment = source.substr(0, source.indexOf('*/') + 2);
source = source.substr(comment.length).replace(/^\s+/, '');
trailing_comments += comment + "\n";
} else if (/^\s*\/\//.test(source)) {
found = true;
comment = source.match(/^\s*\/\/.*/)[0];
source = source.substr(comment.length).replace(/^\s+/, '');
trailing_comments += comment + "\n";
}
} while (found);
return trailing_comments + source;
}
function beautify(elem) {
if (the.beautify_in_progress) return;
the.beautify_in_progress = true;
// var source = $('#source').val();
var source = $(elem).html();
var indent_size = $('#tabsize').val();
var indent_char = indent_size == 1 ? '\t' : ' ';
var preserve_newlines = $('#preserve-newlines').attr('checked');
var keep_array_indentation = $('#keep-array-indentation').attr('checked');
var brace_style = $('#brace-style').val();
if ($('#detect-packers').attr('checked')) {
source = unpacker_filter(source);
}
var comment_mark = '<-' + '-';
var opts = {
indent_size: indent_size,
indent_char: indent_char,
preserve_newlines:preserve_newlines,
brace_style: brace_style,
keep_array_indentation:keep_array_indentation,
space_after_anon_function:true};
if (source && source[0] === '<' && source.substring(0, 4) !== comment_mark) {
$(elem).html(
style_html(source, opts)
);
} else {
var v = js_beautify(unpacker_filter(source), opts);
$(elem).html(v);
}
the.beautify_in_progress = false;
}
第二步:在自己的jsp或html头部引入该两个js文件:
<script type="text/javascript" src="beautify.js"></script>
<script type="text/javascript" src="beautify-html.js"></script><xmp class="beautify">你需要美化的javascript脚本</xmp>第三步:在自己的js中定义方法如下:var the = { beautify_in_progress: false };
//this dummy function alleviates Chrome large string corruption by probably shoveling the corruption bug to some other area
if (/chrome/.test(navigator.userAgent.toLowerCase())) {
String.prototype.old_charAt = String.prototype.charAt;
String.prototype.charAt = function (n) { return this.old_charAt(n); }
}
function unpacker_filter(source) {
var trailing_comments = '';
var comment = '';
var found = false;
do {
found = false;
if (/^\s*\/\*/.test(source)) {
found = true;
comment = source.substr(0, source.indexOf('*/') + 2);
source = source.substr(comment.length).replace(/^\s+/, '');
trailing_comments += comment + "\n";
} else if (/^\s*\/\//.test(source)) {
found = true;
comment = source.match(/^\s*\/\/.*/)[0];
source = source.substr(comment.length).replace(/^\s+/, '');
trailing_comments += comment + "\n";
}
} while (found);
return trailing_comments + source;
}
function beautify(elem) {
if (the.beautify_in_progress) return;
the.beautify_in_progress = true;
// var source = $('#source').val();
var source = $(elem).html();
var indent_size = $('#tabsize').val();
var indent_char = indent_size == 1 ? '\t' : ' ';
var preserve_newlines = $('#preserve-newlines').attr('checked');
var keep_array_indentation = $('#keep-array-indentation').attr('checked');
var brace_style = $('#brace-style').val();
if ($('#detect-packers').attr('checked')) {
source = unpacker_filter(source);
}
var comment_mark = '<-' + '-';
var opts = {
indent_size: indent_size,
indent_char: indent_char,
preserve_newlines:preserve_newlines,
brace_style: brace_style,
keep_array_indentation:keep_array_indentation,
space_after_anon_function:true};
if (source && source[0] === '<' && source.substring(0, 4) !== comment_mark) {
$(elem).html(
style_html(source, opts)
);
} else {
var v = js_beautify(unpacker_filter(source), opts);
$(elem).html(v);
}
the.beautify_in_progress = false;
}
$(function(){ //js美化 $('.beautify').each(function(){ beautify(this); }); });
相关文章推荐
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
- 测试JavaScript字符串处理性能的代码
- JavaScript_学习 6_字符串处理,String对象的字符串格式化方法
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
- Dreamweaver CS5 CS6 代码格式化、美化插件(可同一时候格式化HTML、JavaScript、CSS )眼下最好用的代码格式化扩展
- android代码重构日记(三)——字符串资源处理及其格式化
- Dreamweaver CS5 CS6 代码格式化、美化插件(可同一时候格式化HTML、JavaScript、CSS )眼下最好用的代码格式化扩展
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
- Dreamweaver CS5 CS6 代码格式化、美化插件(可同时格式化HTML、JavaScript、CSS )目前最好用的代码格式化扩展
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
- HTML-DEV-ToolLink(常用的在线字符串编解码、代码压缩、美化、JSON格式化、正则表达式、时间转换工具、二维码生成与解码等工具,支持在线搜索和Chrome插件。)
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
- android代码重构之字符串资源处理及其格式化
- JS Nice – JavaScript 代码美化和格式化工具
- JS Nice – JavaScript 代码美化和格式化工具
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具