Client-side equivalents of Server.HTMLEncode() and Server.HTMLDecode()?

Does anybody have javascript functions handy that will take in a string and return the HTML encoded version of the string, and/or the other way around?

I am writing a translation module, and some strings from the database contain HTML tags -- in this module they should be displayed literally.

I can't do it on the server -- it's a long story why. I also don't want to do postbacks or Ajax server trips, it needs to be entirely client-side.

Thanks very much in advance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jens FiedererTest Developer/ValidatorCommented:
The escape() function can be used to encode urls.  Modern people use encodeURI
slavkomaeAuthor Commented:
The thing is, I don't want to encode a URL. I want to be able to explicitly display a string that may have HTML tags in the UI. Then I have an edit control that should display it as it is (decoded), and when I'm done editing I want to encode it back again.
Jens FiedererTest Developer/ValidatorCommented:
It's pretty much the same thing, except that more gets encoded (spaces are encoded, not just special characters).

HTMLEncode("This is a <Test String>.") gives "This is a %3cTest String%3e."

while escape("This is a <Test String>.") gives "This%20is%20a%20%3cTest%20String%3e."

unescape should bring either of those back to the original.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

slavkomaeAuthor Commented:
Right -- except that I need to display the encoded information in the UI and have it not look encoded, and I don't feel comfortable simply doing a string replace for the spaces because I have no idea what different browsers may or may not do with this function. I'm comfortable assuming that unencoding an encoded string will get you back to where you began -- but I don't want to do any additional steps in the process, because along the way I have to do comparisons, replacements, etc.

Actually, HTMLEncode will give you "This is a &lt;Test String&gt;." as far as I know. That's what I'm looking for.
Jens FiedererTest Developer/ValidatorCommented:
According to the 2005 MSDN for

 HttpServerUtility.HtmlEncode Method (String)

The following code example encodes a string for transmission by HTTP. It encodes the string named TestString, which contains the text "This is a <Test String>.", and copies it into the string named EncodedString as "This is a %3cTest String%3e.".

Dim TestString As String = "This is a <Test String>."
Dim EncodedString As String = Server.HtmlEncode(TestString)

Jens FiedererTest Developer/ValidatorCommented:
OK, the
HttpUtility.HtmlEncode Method (String)
looks like the one you are talking about.
slavkomaeAuthor Commented:
Yes, exactly. I think you're talking about a static method in HttpServerUtility... I was talking about the dynamic method on the Page.Server object.

Except I need it replicated on the client. :-)
Jens FiedererTest Developer/ValidatorCommented:
has a couple of encode implementations.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slavkomaeAuthor Commented:
Thanks so much!
I think this will always work, and it doesn't require a downloadable library...
function htmlEncode(s){
	var elm = document.createElement("div");
	elm.innerText = elm.textContent = s;
	s = elm.innerHTML;
	delete elm;
	return s;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.