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.