HTML DOM Location pathname 屬性


Location pathname 屬性返回/設定對應於 URL 路徑名的字串。

語法

以下為語法 −

  • 返回 **pathname **屬性的值
location.pathname
  • href 屬性設定的值
location.pathname = pathOfHost

示例

讓我們看一個 **Location pathname** 屬性的示例 −

 線上演示

<!DOCTYPE html>
<html>
<head>
<title>Location pathname</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>Location-pathname</legend>
<label for="urlSelect">Current URL:</label>
<input type="url" size="30" id="urlSelect" value="https://www.example.com:2544/aboutUs/CTO.htm">
<input type="button" onclick="getpathname()" value="Get pathname">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var urlSelect = document.getElementById("urlSelect");
   function getpathname(){
      divDisplay.textContent = 'URL pathname: '+location.pathname;
   }
</script>
</body>
</html>

輸出

這將產生以下輸出 −

點選 **‘獲取路徑名’** 按鈕之前 −

點選 **‘獲取路徑名’** 按鈕之後 −

更新於: 30-7-2019

82 次瀏覽

開啟您的 職業旅程

完成本課程,獲得認證

開始學習
廣告
© . All rights reserved.