LoginSignup
0
0

More than 3 years have passed since last update.

【JavaScript】location.pathnameプロパティでURLのパス名を扱う方法

Posted at

プログラミング勉強日記

2020年12月14日
location.pathnameプロパティでURLのパス名を取得したり変更したりする方法をまとめる。

location.pathnameプロパティとは

 現在のページのURLのパス名を参照することができる。location.pathnameで現在のパス名を取得して、location.pathname="pathname"でパス名を指定してページを遷移させられる。

 URLのパス名は、URLの中でファイルの場所を示す部分である。https://qiita.com/mzmz__02の場合パス名はmzmz__02の部分である。

URLのパス名を取得・変更する方法

// URLのパス名を取得
var pathName = location.pathname;

// URLのパス名を変更
location.pathname = "/newPath/test.html"

参考文献

location.pathname …… 現在ページURLのパス名を参照する
JavaScriptでlocation.pathnameプロパティを使ってURLのパス名を取得・変更する方法を現役エンジニアが解説【初心者向け】

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