HTML DOM Location port Property


The Location port property returns/sets the port number (if specified) for a URL. Port number might not get displayed if not explicitly specified.

Syntax

Following is the syntax −

  • Returning value of the port property
location.port
  • Value of the port property set
location.port = portNumber

Example

Let us see an example for Location port property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Location port</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-port</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="getport()" value="Get Port">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var urlSelect = document.getElementById("urlSelect");
   function getport(){
      divDisplay.textContent = 'URL port: '+location.port;
   }
</script>
</body>
</html>

Output

This will produce the following output −

Before clicking ‘Get Port’ button −

After clicking ‘Get Port’ button −

Updated on: 30-Jul-2019

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements