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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.