概念と使用方法
encodeURI()関数はURL構造を定義する一部の特殊文字を除いてURLをエンコードします。 この関数は主にURL全体の文字列をエンコードする際に使用されます。基本の例
const uri = "https://www.example.com/?x=サンプル値";
const encoded = encodeURI(uri);
console.log(encoded);
// 出力: "https://www.example.com/?x=%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E5%80%A4"
返り値
指定された文字列をURIとしてエンコードした新しい文字列を返します。例
const uri = "https://example.com/search?query=Hello, world!&page=1";
const encodedUri = encodeURI(uri);
console.log(encodedUri);
// 出力: "https://example.com/search?query=Hello%2C%20world!&page=1"
この例では、encodeURI()関数はuri変数に格納されたURIをエンコードします。エンコードされない特殊文字は、:、/、?、&、=、!などです。