endecode.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. _convert();
  35. });
  36. jQuery("#btnCodeClear").click(function(){
  37. jQuery("#srcText,#rstCode").val("")
  38. });
  39. };
  40. /**
  41. * 每个单选按钮被点击时,都自动进行转换
  42. */
  43. var _bindRadioEvent = function(){
  44. jQuery("input[type=radio],label[for]").click(function(evt){
  45. $this = jQuery(this);
  46. setTimeout(function(){
  47. _convert();
  48. },150);
  49. });
  50. };
  51. /**
  52. * 鼠标划过结果框,选中
  53. */
  54. var _bindRstEvent = function(){
  55. jQuery("#rstCode").mouseover(function(){
  56. this.selectionStart = 0;
  57. this.selectionEnd = this.value.length;
  58. this.select();
  59. });
  60. };
  61. var _init = function(){
  62. jQuery(function(){
  63. //输入框聚焦
  64. jQuery("#srcText").focus();
  65. //绑定按钮的点击事件
  66. _bindBtnEvent();
  67. //鼠标划过结果框,选中
  68. _bindRstEvent();
  69. //单选按钮的点击事件
  70. _bindRadioEvent();
  71. });
  72. };
  73. return {
  74. init : _init
  75. };
  76. })();
  77. //初始化
  78. baidu.ed.init();