Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JavaScript»How to Get Current URL in Web Browser using JavaScript

    How to Get Current URL in Web Browser using JavaScript

    By RahulMarch 4, 20232 Mins Read

    When building web applications, it’s often necessary to access and manipulate the current URL in the web browser. JavaScript provides a simple and easy way to get the current URL in the browser, which can be useful for a wide range of purposes, from tracking user behavior to implementing dynamic navigation menus.

    Advertisement

    In this article, we’ll explore how to get the current URL in the web browser using JavaScript. We’ll cover two methods: using the window.location property and using the document.URL property.

    Method 1: Using the window.location Property

    The “window.location” property provides information about the current URL in the browser. This property includes various information about the URL, such as the protocol, hostname, port number, path, and query string.

    To get the current URL using the “window.location” property, simply access the property and retrieve the “href” attribute. Here’s an example code snippet:

    1
    2
    var currentURL = window.location.href;
    console.log(currentURL);

    In this example, we first declare a variable called currentURL. We then assign it the value of the “window.location.href” attribute, which contains the current URL. Finally, we use the console.log method to print the current URL to the console.

    Method 2: Using the document.URL Property

    The “document.URL” property provides the current URL of the document, including the protocol, hostname, port number, path, and query string. This property is similar to the window.location property, but provides a more direct way to access the URL.

    To get the current URL using the document.URL property, simply access the property and retrieve its value. Here’s an example code snippet:

    1
    2
    var currentURL = document.URL;
    console.log(currentURL);

    In this example, we first declare a variable called currentURL. We then assign it the value of the document.URL property, which contains the current URL. Finally, we use the console.log method to print the current URL to the console.

    Conclusion

    Getting the current URL in the web browser using JavaScript is a simple and useful task for web developers. By using the window.location property or the document.URL property, developers can easily access and manipulate the current URL for a wide range of purposes. Whether it’s for tracking user behavior or implementing dynamic navigation menus, knowing how to get the current URL is an essential skill for any web developer.

    javascript js location url
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    20 Common JavaScript Interview Questions and Answers

    JavaScript Arrays Tutorial for Beginner's

    JavaScript Arrays: A Beginner’s Guide

    How to Get the Current Timestamp in JavaScript

    View 6 Comments

    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

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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