Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JavaScript»How to Get Current URL in Web Browser using JavaScript

    How to Get Current URL in Web Browser using JavaScript

    RahulBy RahulFebruary 15, 20172 Mins ReadUpdated:June 10, 2022

    JavaScript provides multiple methods to get the current URL displayed in the web browser address bar. You can use the Location object property of the Window object to get these details. Below is the list of a few properties of the location objects.

    Here is the list of options available to get URLs and other details using JavaScript. The first example will get the current URL in a web browser. Other examples are also available for your reference.

    #1 – href

    This will return the entire URL displayed in the address bar.

    1
    var currentURL = window.location.href;

    #2 – host

    This will return the hostname and port of the URL in the address bar.

    1
    var currentHost = window.location.host;

    #3 – hostname

    This will return only the hostname of the URL in the address bar.

    1
      var currentHostname = window.location.hostname;

    #4 – port

    This will return only the port detail of the URL in the address bar.

    1
      var currentPort = window.location.port;

    #5 – protocol

    This will return the protocol of the URL in the address bar. Like the URL is using HTTP (without SSL) or HTTPS (with SSL).

    1
      var currentProtocol = window.location.protocol;

    #6 – pathname

    This will return the pathname (value after the domain name) of the URL in the address bar.

    1
      var currentPathname = window.location.pathname;

    #7 – hash

    This will return the anchor portion of the URL including the hash sign (#).

    1
      var currenthash = window.location.hash;

    #8 – search

    This will return the query portion of the URL-like portion started with the question mark (?).

    1
      var currentSearchString = window.location.search;

    javascript js location url
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Check System Uptime in Linux
    Next Article How to Use SSH to Connect to a Remote Server

    Related Posts

    How to Replace String in JavaScript

    Updated:June 13, 20222 Mins Read

    JavaScript Program to Add Two Numbers

    Updated:June 9, 20222 Mins Read

    How to Install ReactJS on Ubuntu 20.04

    3 Mins Read

    Remove First Character from String in JavaScript

    1 Min Read

    10 Useful JavaScript Console Methods

    Updated:June 9, 20223 Mins Read

    JavaScript Program to Remove Duplicate Array Elements

    Updated:July 20, 20211 Min Read

    6 Comments

    1. puneet on June 16, 2021 8:34 am

      I’ve tested this with an iframe embedded in a page in freshdesk.
      It returns the URL of the iframe and not what’s in the address bar of the browser.

      Reply
    2. Henri Parfait on April 12, 2021 1:10 pm

      thank you this post is wonderful 🙂

      Reply
    3. Steve on February 17, 2021 11:04 am

      Hmmm…. I’ve tested this with an iframe embedded in a page and ran the query within the iframe page.
      It returns the URL of the iframe and not what’s in the address bar of the browser.

      Might need tweaking using parent.location – but I have no idea, sorry.

      Reply
      • Steve on February 17, 2021 11:11 am

        Sorry, meant top.location, not parent.location

        Reply
    4. Duke on December 14, 2019 12:59 pm

      Thank you. This is extremely useful. BTW, in #4 I think you meant – port. But the code is right and that’s what matters!

      Reply
    5. Harry Watson on November 24, 2017 11:49 am

      We provide solution regarding Google chrome. If your Google chrome web browser is not responding then visit us. We provide full assistance regarding it.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.