w3resource logo


JavaScript window Object - btoa Method

JavaScript window Object : btoa() Method

rating Average rating 9 out of 10. Total 4 users rated.

<<PreviousNext>>

Description

The btoa() method of window object is used to convert a given string to a encoded data (using base-64 encoding) string.

Version

Implemented in JavaScript 1.2

Syntax

btoa(string-to-encode)

Parameter

string-to-encode : String to be encoded.

Example of window Object : btoa() method

In the following web document btoa() method converts a given string to a encoded string.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript window object - btoa() method example</title>
</head>
<body>
<h1 style="color: red">JavaScript window object : btoa() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
string1="Hello, world"
document.write('Before encoding the string is : '+string1 + "<br />") 
encodedData = btoa(string1)
document.write('After encoding the string is : '+encodedData + "<br />")
//]]>
</script>
</body>
</html>

View the example of window object's btoa method in the browser

Supported Browser

Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes

See also :

JavaScript Client side objects, methods, properties.

JavaScript Core objects, methods, properties.



<<PreviousNext>>