ページのURLやディレクトリ、ファイル名を取得するサンプル



ページ情報はlocationで取得
//url
location.href

//hostname
location.hostname

//pathname
location.pathname

//search
location.search

テスト用サンプル
//url
$("#sample").append('<p>url:' + location.href + '</p>');

//hostname
$("#sample").append('<p>hostname:' + location.hostname + '</p>');

//pathname
$("#sample").append('<p>pathname:' + location.pathname + '</p>');

//search
$("#sample").append('<p>search:' + location.search + '</p>');

//ファイル名
var filename = location.href.match(".+/(.+?)([\?#;].*)?$")[1];
$("#sample").append('<p>filename:' + filename  + '</p>');



テスト用サンプル実行結果


0 件のコメント :

コメントを投稿