Originally published by Nick Major at https://coderrocketfuel.com
To get the URL of the page you're currently on, JavaScript offers the Location
interface that is exposed by the global Window
object by your browser.
And you can access the Location
interface with:
window.location
And since Window is a global object in the browser, you can access it using this also:
location
And if you want the href
of the current page, use this:
location.href // "https://coderrocketfuel.com/article/get-the-url-of-the-current-page-using-javascript"
There are also a ton of other useful properties in the location object.
Here are some examples for the page you are currently on:
location.protocol // "https:" location.host // "coderrocketfuel.com" location.hostname // "coderrocketfuel.com" location.port // "" location.pathname // "/article/get-the-url-of-the-current-page-using-javascript" location.origin // "https://coderrocketfuel.com"
You can read more about both the Window and Location interfaces on the Mozilla Developer Web Docs.
As always, thanks for reading and happy coding!
Thanks for reading ❤
If you liked this post, please do share/like it with all of your programming buddies!
Follow us on Facebook | Twitter
☞ The Complete JavaScript Course 2019: Build Real Projects!
☞ Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
☞ JavaScript Bootcamp - Build Real World Applications
☞ JavaScript Programming Tutorial - Full JavaScript Course for Beginners
☞ New ES2019 Features Every JavaScript Developer Should Know
☞ Best JavaScript Frameworks, Libraries and Tools to Use in 2019
☞ JavaScript Basics Before You Learn React
☞ Build a CMS with Laravel and Vue
#javascript #web-development