HTML DOM Location pathname Property

HTMLWeb DevelopmentFront End Technology

The Location pathname property returns/sets the string corresponding to the URL pathname.

Syntax

Following is the syntax −

  • Returning value of the pathname property
location.pathname
  • Value of the href property set
location.pathname = pathOfHost

Example

Let us see an example for Location pathname property −

 Live Demo

<!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>

Output

This will produce the following output −

Before clicking ‘Get pathname’ button −

After clicking ‘Get pathname’ button −

raja
Published on 30-Jul-2019 17:42:19
Advertisements