練炭ブログ

萌え壁紙、Irvine、DMonkey、Proxomitron などの情報を扱ってます。

DMonkey: URL.expand()

コメントなし»

URL オブジェクトの expand() メソッドの挙動を調べてみました。

var u = 'http://example.com/dir/file?foo=123';

alert ((new URL (u)).expand ('abc'));
// http://example.com/dir/abc

alert ((new URL (u)).expand ('abc;def'));
// http://example.com/dir/abc

alert ((new URL (u)).expand ('?bar=456'));
// http://example.com/dir/?bar=456

alert ((new URL (u)).expand ('/'));
// http://example.com/

alert ((new URL (u)).expand ('./'));
// http://example.com/dir/

alert ((new URL (u)).expand ('../'));
// http://example.com/

alert ((new URL (u)).expand ('http://example.jp/'));
// http://example.jp/

alert ((new URL (u)).expand ('//example.jp/'));
// http://example.com//example.jp/

  • 引数の ; 以降が削除される。
  • 引数が ? からはじまると(query のみ)、filename が空として解釈される。
  • 引数が // からはじまると(net_path)、絶対パス(abs_path)として解釈される。

HTML ページ内の相対 URL を絶対 URL に整形するのに使えればと思っていたのですが、自前で書くしかないようです。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です