TextDecoder Web API | TextDecoder constructor
In HTML there is a TextDecoder Interface of which we can create a TextDecoder object for the encoding specified in parameter.
Syntax:
decoder = new TextDecoder( utf-Label, option );
Parameters: This constructor accepts two parameters which are mentioned above and described below:
- utf-Label: Label of the encoder in string format. It is “utf-8” as default.
- option: TextDecoderOptions dictionary which has a property fatal (It is a Boolean flag which indicates if the TextDecoder.decode() method should throw DOMException. It’s default value is false.)
Example 1: This example creating a TextDecoder object with “iso-8859-2” as parameter.
<!DOCTYPE html>
< html >
< head >
< title >
TextDecoder Web API | TextDecoder constructor
</ title >
</ head >
< body >
< center >
< h1 style = "color:green;" >
GeeksforGeeks
</ h1 >
< h2 >TextDecoder constructor</ h2 >
< button onclick = "gettextDecoder ();" >
Get textDecoder object
</ button >
< p id = 'textDecoder' ></ p >
< script type = "text/javascript" >
function gettextDecoder() {
var textDecoder1 = new TextDecoder("iso-8859-2");
console.log(textDecoder1);
}
</ script >
</ center >
</ body >
</ html >
|
Output:
- Before Click the button:
- After Click the button:
Example: Create a comment object with nothing as parameter. So the default parameter which is “utf-8” will be taken.
<!DOCTYPE html>
< html >
< head >
< title >
TextDecoder Web API | TextDecoder constructor
</ title >
</ head >
< body >
< center >
< h1 style = "color:green;" >
GeeksforGeeks
</ h1 >
< h2 >TextDecoder constructor</ h2 >
< button onclick = "gettextDecoder ();" >
Get textDecoder object
</ button >
< p id = 'textDecoder' ></ p >
< script type = "text/javascript" >
function gettextDecoder() {
var textDecoder1 = new TextDecoder();
console.log(textDecoder1);
}
</ script >
</ center >
</ body >
</ html >
|
Output:
- Before Click the button:
- After Click the button:
Supported Browsers: The browsers supported by TextDecoder constructor are listed below:
- Google Chrome 38
- Firefox 19
- Opera 25
- Safari 10.1
Last Updated :
04 Oct, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...