location の属性メモ
{window,document}.location.href には表示しているページのURL全体が格納されているというのは即座に出てくるのだが、それ以外の属性がすぐに出てこないのでメモ。
確認のためのグリモンスクリプト。
(function() { alert( '' + '\n' + 'href: ' + location.href + '\n' + 'host: ' + location.host + '\n' + 'hostname: ' + location.hostname + '\n' + 'port: ' + location.port + '\n' + 'protocol: ' + location.protocol + '\n' + 'pathname: ' + location.pathname + '\n' + 'search: ' + location.search + '\n' + 'hash: ' + location.hash ); })();
で、次のURLにアクセスする。
http://192.168.0.1:8080/hoge/foo/bar?id=foobar&name=barhoge#ukeke
そして結果。
href: http://192.168.0.1:8080/hoge/foo/bar?id=foobar&name=barhoge#ukeke host: 192.168.0.1:8080 hostname: 192.168.0.1 port: 8080 protocol: http: pathname: /hoge/foo/bar search: ?id=foobar&name=barhoge hash: #ukeke
ポート番号が指定されない場合:
http://192.168.0.1/hoge/foo/bar?id=foobar&name=barhoge#ukeke
だとこんな感じ。("http:" で ":80" を指定した場合でも、ブラウザが勝手に ":80" を取り去っちゃうので同じ。)
href: http://192.168.0.1/hoge/foo/bar?id=foobar&name=barhoge#ukeke host: 192.168.0.1 hostname: 192.168.0.1 port: protocol: http: pathname: /hoge/foo/bar search: ?id=foobar&name=barhoge hash: #ukeke