jQueryでURLやパラメータを取得する
どうも、こんにちは。
Webデザイナーのなお(@ENOWL_Nao)です。
直近の案件でJavaScriptでURLを取得して処理をすることがありました。
毎回調べているので、自分用にメモを残しておきたいと思います。
現在のURLを取得する
現在のページのURLを取得するには、$(location).attr(‘href’)を使用します。
https://enowl.net/jquery/demo/index.html
1<!DOCTYPE >
2<html lang="ja">
3 <head>
4 <title>現在のURLを取得する</title>
5 </head>
6 <body>
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
8 <script>
9 const pageUrl = $(location).attr('href');
10 console.log(pageUrl);
11 </script>
12 </body>
13</html>https://enowl.net/jquery/demo/index.html
URLのプロトコルを取得する
URLのプロトコルを取得するには、$(location).attr(‘protocol’)を使用します。
https://enowl.net/jquery/demo/index.html
1<!DOCTYPE >
2<html lang="ja">
3 <head>
4 <title>URLのプロトコルを取得する</title>
5 </head>
6 <body>
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
8 <script>
9 const pageProtocol = $(location).attr('protocol');
10 console.log(pageProtocol);
11 </script>
12 </body>
13</html>https:
URLのホスト名を取得する
URLのホスト名を取得するには、$(location).attr(‘hostname’)を使用します。
https://enowl.net/jquery/demo/index.html
1<!DOCTYPE >
2<html lang="ja">
3 <head>
4 <title>URLのホスト名を取得する</title>
5 </head>
6 <body>
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
8 <script>
9 const pageHostName = $(location).attr('hostname');
10 console.log(pageHostName);
11 </script>
12 </body>
13</html>enowl.net
URLのホスト名を取得する(ポート番号も含む)
URLのホスト名を取得するには、$(location).attr(‘host’)を使用します。
こちらはポート番号も取得します。
https://enowl.net:8080/jquery/demo/index.html
1<!DOCTYPE >
2<html lang="ja">
3 <head>
4 <title>URLのホスト名を取得する(ポート番号も含む)</title>
5 </head>
6 <body>
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
8 <script>
9 const pageHost = $(location).attr('host');
10 console.log(pageHost);
11 </script>
12 </body>
13</html>enowl.net:8080
URLのポート番号を取得する
ポート番号を取得するには、$(location).attr(‘port’)を使用します。
https://enowl.net:8080/jquery/demo/index.html
1<!DOCTYPE >
2<html lang="ja">
3 <head>
4 <title>URLのポート番号を取得する</title>
5 </head>
6 <body>
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
8 <script>
9 const pagePort = $(location).attr('port');
10 console.log(pagePort);
11 </script>
12 </body>
13</html>8080
URLのパス部分を取得する
URLのパス部分を取得するには、$(location).attr(‘pathname’)を使用します。
https://enowl.net:8080/jquery/demo/index.html
1<!DOCTYPE >
2<html lang="ja">
3 <head>
4 <title>URLのパス部分を取得する</title>
5 </head>
6 <body>
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
8 <script>
9 const pagePathName = $(location).attr('pathname');
10 console.log(pagePathName);
11 </script>
12 </body>
13</html>/jquery/demo/index.html
URLのパラメータを取得する
URLのパラメータを取得するには、$(location).attr(‘search’)を使用します。
https://enowl.net:8080/jquery/demo/index.html?page=2
1<!DOCTYPE >
2<html lang="ja">
3 <head>
4 <title>URLのパラメータを取得する</title>
5 </head>
6 <body>
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
8 <script>
9 const pageSearch = $(location).attr('search');
10 console.log(pageSearch);
11 </script>
12 </body>
13</html>?page=2
URLの#~を取得する
URLの#~(アンカーリンク)を取得するには、$(location).attr(‘hash’)を使用します。
https://enowl.net:8080/jquery/demo/index.html#about
1<!DOCTYPE >
2<html lang="ja">
3 <head>
4 <title>URLの#~を取得する</title>
5 </head>
6 <body>
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
8 <script>
9 const pageHash = $(location).attr('hash');
10 console.log(pageHash);
11 </script>
12 </body>
13</html>about
URLのプロトコルとポートを取得する
URLのプロトコルとポートを取得するには、$(location).attr(‘origin’)を使用します。
https://enowl.net:8080/jquery/demo/index.html#about
1<!DOCTYPE >
2<html lang="ja">
3 <head>
4 <title>URLのプロトコルとポートを取得する</title>
5 </head>
6 <body>
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
8 <script>
9 const pageOrigin = $(location).attr('origin');
10 console.log(pageOrigin);
11 </script>
12 </body>
13</html>https://enowl.net:8080
まとめ
jQueryでURLやパラメータを取得する方法でした。
パラメータを取得して処理を分けたりすることもあるので、これを見ながらJS書いていきたいと思います。
ここまで読んでいただきありがとうございました!


