0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

URL クエリパラメータの値が日本語で Safari だけ挙動がおかしいときの対処法

Last updated at Posted at 2021-03-09

URL クエリパラメータの値が日本語で、その日本語の値を JavaScript で処理する際に、Safari と Safari 以外のブラウザとで挙動が異なりしばらくハマりました。今回は、そのとき取った対処法を共有します。

原因は、Unicode の正規化形式の違いでした。

UTF-8 にはさらに細かい種類があって、一般的に UTF-8 というと NFC でエンコードされたものを指すことが多いです。一方 macOS や iOS では、NFD でエンコードされたもの(UTF-8-MAC)を指します。

私が直面した問題は、ある JavaScript のプログラムを macOS ないしは iOS で実行したときに、クエリパラメータの値(日本語)が、Safari 以外では NFD で正規化されてパーセントエンコーディングされ、Safari の場合は NFC で正規化されてパーセントエンコーディングされてしまうというものでした。

Chrome や Firefox では全く再現しません。Safari では頻度こそ高いものの毎回ではないので、どのような条件で起こるのか完全な特定まではできていませんが、URL を直打ちしたときに最も再現率が高かったように思います。

以上のことから、以下のように対処して解決しました。

let userAgent = window.navigator.userAgent;
if (userAgent.indexOf('Safari') != -1) {
  str = str.normalize('NFD');
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?