endecode.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * 注册命名空间:baidu.ed
  3. */
  4. baidu.namespace.register("baidu.ed");
  5. baidu.ed = (function(){
  6. /**
  7. * 转码
  8. */
  9. var _convert = function(){
  10. var srcText = jQuery("#srcText").val();
  11. jQuery("#rst").show();
  12. var rstCode = jQuery("#rstCode");
  13. if (jQuery("#uniEncode").attr("checked") == true) {
  14. rstCode.val(baidu.endecode.uniEncode(srcText));
  15. } else if(jQuery("#uniDecode").attr("checked") == true){
  16. rstCode.val(baidu.endecode.uniDecode(srcText));
  17. } else if(jQuery("#utf8Encode").attr("checked") == true){
  18. rstCode.val(encodeURIComponent(srcText));
  19. } else if(jQuery("#utf8Decode").attr("checked") == true){
  20. rstCode.val(decodeURIComponent(srcText));
  21. } else if(jQuery("#base64Encode").attr("checked") == true){
  22. rstCode.val(baidu.endecode.base64Encode(baidu.endecode.utf8Encode(srcText)));
  23. } else if(jQuery("#base64Decode").attr("checked") == true){
  24. rstCode.val(baidu.endecode.utf8Decode(baidu.endecode.base64Decode(srcText)));
  25. } else {
  26. rstCode.val(hex_md5(srcText));
  27. }
  28. };
  29. /**
  30. * 绑定按钮的点击事件
  31. */
  32. var _bindBtnEvent = function(){
  33. jQuery("#btnCodeChange").click(function(){
  34. //统计
  35. baidu.log.track(LOG.endecode_page_btnchange);
  36. _convert();
  37. });
  38. jQuery("#btnCodeClear").click(function(){
  39. //统计
  40. baidu.log.track(LOG.endecode_page_btnclear);
  41. jQuery("#srcText,#rstCode").val("")
  42. });
  43. };
  44. /**
  45. * 每个单选按钮被点击时,都自动进行转换
  46. */
  47. var _bindRadioEvent = function(){
  48. jQuery("input[type=radio],label[for]").click(function(evt){
  49. $this = jQuery(this);
  50. setTimeout(function(){
  51. //统计
  52. baidu.log.track(LOG["endecode_page_" + $this.attr("id")]);
  53. _convert();
  54. },150);
  55. });
  56. };
  57. /**
  58. * 鼠标划过结果框,选中
  59. */
  60. var _bindRstEvent = function(){
  61. jQuery("#rstCode").mouseover(function(){
  62. this.selectionStart = 0;
  63. this.selectionEnd = this.value.length;
  64. this.select();
  65. });
  66. };
  67. var _init = function(){
  68. jQuery(function(){
  69. //统计
  70. baidu.log.track(LOG.endecode_page_opened);
  71. //输入框聚焦
  72. jQuery("#srcText").focus();
  73. //绑定按钮的点击事件
  74. _bindBtnEvent();
  75. //鼠标划过结果框,选中
  76. _bindRstEvent();
  77. //单选按钮的点击事件
  78. _bindRadioEvent();
  79. });
  80. };
  81. return {
  82. init : _init
  83. };
  84. })();
  85. //初始化
  86. baidu.ed.init();