• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Get the URL Path

Using JavaScript to Get the URL Path

July 13, 2022 Leave a Comment

We can use JavaScript to get the URL path of the current page we are on by using the window object and getting the location.pathname property of it. So the property window.location.pathname will return the current URL path that you see at the top of your browser.

var curr_path_url = window.location.pathname;

console.log(curr_path_url);

#Output
/javascript-get-url-path/

Using JavaScript to Get the Base URL

We can also use JavaScript to get the base URL of the current page we are on by using the window object and getting the location.base property of it. So the property window.location.base will return the current base URL that you see at the top of your browser.

var curr_base_url = window.location.base;

console.log(curr_base_url);

#Output
//https://theprogrammingexpert.com

Using JavaScript to Get the Host URL

We can also use JavaScript to get the host URL of the current page we are on by using the window object and getting the location.host property of it. So the property window.location.host will return the current host URL that you see at the top of your browser.

var curr_host_url = window.location.host;

console.log(curr_host_url);

#Output
theprogrammingexpert.com

Using JavaScript to Get the Current URL

When working with web browsers, sometimes you may want to get the current URL of the page you are working with after getting that web page with your web driver.

We can easily get the current URL of a web page by accessing the window object and getting it’s location.href property.

var curr_url = window.location.href;

console.log(curr_url);

#Output
//https://theprogrammingexpert.com/javascript-get-host-url/

Hopefully this article has been useful for you to learn how to use JavaScript to get the host url of a webpage.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Get a Random Number Between 1 and 20
  • 2.  Using JavaScript to Get the Page Title
  • 3.  Using JavaScript to Convert Double to Integer
  • 4.  Using JavaScript to Compare Dates
  • 5.  Using JavaScript to Convert String to Boolean
  • 6.  Using JavaScript to Remove Forward Slash From String
  • 7.  JavaScript Power Operator
  • 8.  Using JavaScript to Check if String Contains Only Numbers
  • 9.  Using JavaScript to Check a Radio Button
  • 10.  Using JavaScript to Reverse a Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy