Skip to content
Related Articles

Related Articles

HTML | DOM Location port Property

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 23 Jul, 2019

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


  • Get the port property:
  • location.port
  • Set the port property:
  • location.port = 8080

Property Value:

  • A String, to be assigned as port
  • Return Value:

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

    <!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', '');
            <!-- accessing "url.port". -->
            window.alert("The URL port number of the current page is: "
                         + url.port);

    Output :

    Supported Browsers:

    • Google Chrome
    • Internet Explorer
    • Firefox
    • Opera
    • Safari

    My Personal Notes arrow_drop_up
    Recommended Articles
    Page :

    Start Your Coding Journey Now!