Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

Sunday, April 5, 2015

Base64 encode/decode with Javascript

P
Javascript supports you to base64 encode and decode with sample APIs. Now, let see what it can do

function myFunction() {
    var str = "www.it-4shared.com";
    var enc = window.btoa(str);
    var dec = window.atob(enc);
}

output should be

d3d3Lml0LTRzaGFyZWQuY29t
www.it-4shared.com

NOTE: atob() method is supported in IE10 and later

Sample




Saturday, March 7, 2015

HTML encoding and decoding by using Javascript

P
I think this post is really helpful for blogger, programmer and web designer...Sometimes you want to share a good tutorial about HTML and you want to share it with friends by your blog. You have to use this trick to convert HTML code then you could post up it.






How to do

Therefore I have created a little library of functions designed to help me encode and decode HTML with Javascript which you can download here: Encoder.js.
There are a number of useful functions within the object which I will outline here:

  • HTML2Numerical: Converts HTML entities to their numerical equivalents.
  • NumericalToHTML: Converts numerical entities to their HTML equivalents.
  • numEncode: Numerically encodes unicode characters.
  • htmlDecode: Decodes HTML encoded text to its original state.
  • htmlEncode: Encodes HTML to either numerical or HTML entities. This is determined by the EncodeType property.
  • XSSEncode: Encodes the basic characters used in XSS attacks to malform HTML.
  • correctEncoding: Corrects any double encoded ampersands.
  • stripUnicode: Removes all unicode characters.
  • hasEncoded: Returns true if a string contains html encoded entities within it.

Sample Of Use
//example of using the html encode object

// set the type of encoding to numerical entities e.g & instead of &
Encoder.EncodeType = "numerical";

// or to set it to encode to html entities e.g & instead of &
Encoder.EncodeType = "entity";

// HTML encode text from an input element
// This will prevent double encoding.
var encoded = Encoder.htmlEncode(document.getElementById('input'));

// To encode but to allow double encoding which means any existing entities such as
// & will be converted to &
var dblEncoded = Encoder.htmlEncode(document.getElementById('input'),true);

// Decode the now encoded text
var decoded = Encoder.htmlDecode(encoded);

// Check whether the text still contains HTML/Numerical entities
var containsEncoded = Encoder.hasEncoded(decoded);

Reference