Encode & Decode HTML Entities With htmled.js Library

Category: Javascript | April 15, 2024
Author:SH20RAJ
Views Total:21 views
Official Page:Go to website
Last Update:April 15, 2024
License:MIT

Preview:

Encode & Decode HTML Entities With htmled.js Library

Description:

If you’ve ever worked with HTML, you know the frustration of dealing with those pesky character entities like ‘<‘ and ‘>’.

Well, fear not! htmled is a simple JavaScript library that makes encoding and decoding HTML entities a breeze. It works with both browser and Node.js and currently supports over 2100 HTML entities.

How to use it:

1. Install and import the htmled.js with NPM.

# NPM
$ npm install htmled
// node.js
const htmled = require('htmled');
// browser
<script src="/path/to/htmled.min.js"></script>

2. Convert characters like “<” or “>” into their corresponding HTML entity codes. This ensures these characters are displayed correctly within your HTML code.

const encoded = htmled.encode(`This is Á <b>JavaScript</b> website!`);
// => Output: This is &amp&semi;AMP&semi;Aacute&semi; &LT&semi;b&gt&semi;JavaScript&LT&semi;&sol;b&gt&semi; website&excl&semi;

3. Convert encoded HTML entities back into their original characters. This is useful when working with data retrieved from external sources that might contain encoded characters.

const decoded = htmled.decode('This is &amp&semi;AMP&semi;Aacute&semi; &LT&semi;b&gt&semi;JavaScript&LT&semi;&sol;b&gt&semi; website&excl&semi;');
// => Output: This is Á <b>JavaScript</b> website!

You Might Be Interested In:


Leave a Reply