Window: btoa() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

btoa()Window インターフェイスのメソッドで、 Base64 でエンコードされた ASCII 文字列をバイナリー文字列(すなわち、文字列のそれぞれの文字がバイナリーデータの各バイトとして扱われる文字列)から生成します。

このメソッドを使用すると、通信に支障をきたす可能性のあるデータをエンコードして送信し、その後で Window.atob() メソッドを使用して再度デコードすることができます。 例えば ASCII で 0 から 31 の値のような制御文字をエンコードすることもできます。

構文

js
btoa(stringToEncode)

引数

stringToEncode

エンコードするバイナリー文字列です。

返値

stringToEncode の Base64 表現である ASCII 文字列です。

例外

InvalidCharacterError DOMException

文字列には、1 バイトに収まらない文字が含まれていた場合。詳細は後述の「Unicode 文字列」を参照してください。

js
const encodedData = window.btoa("Hello, world"); // 文字列をエンコード
const decodedData = window.atob(encodedData); // 文字列をデコード

Unicode 文字列

Base64 は、設計上、バイナリーデータを入力として期待します。JavaScript の文字列では、これは各文字が 1 バイトしか占有しない文字列を意味します。したがって、2 バイト以上の文字を含む文字列を btoa() に渡した場合、これはバイナリーデータとはみなされないため、エラーが発生します。

js
const ok = "a";
console.log(ok.codePointAt(0).toString(16)); //   61: 長さ < 1 バイト

const notOK = "✓";
console.log(notOK.codePointAt(0).toString(16)); // 2713: 長さ > 1 バイト

console.log(window.btoa(ok)); // YQ==
console.log(window.btoa(notOK)); // エラー

任意の Unicode テキストを扱うときにこの制限を回避する作業方法については、Base64 の用語集の項目の「Unicode 問題」を参照してください。

仕様書

Specification
HTML
# dom-btoa-dev

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
btoa
Available in workers

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報