Skip to content
Related Articles
Open in App
Not now

Related Articles

HTML | DOM Location port Property

Improve Article
Save Article
  • Last Updated : 02 Sep, 2022
Improve Article
Save Article

The HTML DOM Location Port property returns or sets the port number of the current URL. 


  • Get the port property:
  • Set the port property:

Property Value:

  • A String, to be assigned as port

Return Value:

  • A String, representing the port number of a URL.



<!DOCTYPE html>
    <p id="demo"></p>
    <p><b>Note: </b>If the port number is default
      (80 for http and 443 for https),
      most browsers will display 0 or nothing.
        const url = document.createElement('a');
        url.setAttribute('href', '');
        <!-- Return  Location port Property -->
        window.alert("The URL port number of the current page is: "
                     + url.port);

Output :


Supported Browsers:

  • Google Chrome 1
  • Edge 12
  • Internet Explorer 3
  • Firefox 1
  • Opera 12.1
  • Safari 1
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!