Get the current URL with JavaScript?

All I want is to get the website URL. Not the URL as taken from a link. On the page loading I need to be able to grab the full, current URL of the website and set it as a variable to do with as I please.



answered 9 years ago Zanoni #1

Gets the current page URL:


answered 9 years ago VolkerK #2



As noted in the comments, the line below works, but it is bugged for Firefox.


See URL of type DOMString, readonly.

answered 9 years ago Christoph #3

The same question has been asked less than 24 hours ago. To quote myself:

Use window.location for read and write access to the location object associated with the current frame. If you just want to get the address as a read-only string, you may use document.URL, which should contain the same value as window.location.href.

answered 5 years ago Rohan Patil #4

var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
       currentPageUrlIs = this.href.toString().toLowerCase(); 
       currentPageUrlIs = document.location.toString().toLowerCase();

The above code can also help someone

answered 5 years ago Nikhil Agrawal #5

URL Info Access

JavaScript provides you with many methods to retrieve and change the current URL, which is displayed in the browser's address bar. All these methods use the Location object, which is a property of the Window object. You can create a new Location object that has the current URL as follows:

var currentLocation = window.location;

Basic URL Structure

  • protocol: Specifies the protocol name be used to access the resource on the Internet. (HTTP (without SSL) or HTTPS (with SSL))

  • hostname: Host name specifies the host that owns the resource. For example, A server provides services using the name of the host.

  • port: A port number used to recognize a specific process to which an Internet or other network message is to be forwarded when it arrives at a server.

  • pathname: The path gives info about the specific resource within the host that the Web client wants to access. For example, /index.html.

  • query: A query string follows the path component, and provides a string of information that the resource can utilize for some purpose (for example, as parameters for a search or as data to be processed).

  • hash: The anchor portion of a URL, includes the hash sign (#).

With these Location object properties you can access all of these URL components and what they can set or return:

  • href - the entire URL
  • protocol - the protocol of the URL
  • host - the hostname and port of the URL
  • hostname - the hostname of the URL
  • port - the port number the server uses for the URL
  • pathname - the path name of the URL
  • search - the query portion of the URL
  • hash - the anchor portion of the URL

I hope you got your answer..

answered 4 years ago Dorian #6

Use: window.location.href.

As noted above, document.URL doesn't update when updating window.location. See MDN.

answered 4 years ago Syed Nasir Abbas #7

For complete URL with query strings:


For host URL:


answered 3 years ago kishore #8

  • Use window.location.href to get the complete URL.
  • Use window.location.pathname to get URL leaving the host.

answered 3 years ago Sangeet Shah #9

To get the path, you can use:

alert("Url  ="+document.location);
alert("PathName  ="+ window.location.pathname);// Returns path only
alert("url  ="+window.location.href);// Returns full URL

answered 3 years ago Josip Ivic #10

The way to get the current location object is window.location.

Compare this to document.location, which originally only returned the current URL as a string. Probably to avoid confusion, document.location was replaced with document.URL.

And, all modern browsers map document.location to window.location.

In reality, for cross-browser safety, you should use window.location rather than document.location.

answered 2 years ago Maleen Abewardana #11

In jstl we can access the current URL path using pageContext.request.contextPath. If you want to do an Ajax call, use the following URL.

url = "${pageContext.request.contextPath}" + "/controller/path"

Example: For the page this will give

answered 2 years ago Bhaskar Bhatt #12

You can get the current URL location with a hash tag by using:


 // Using href
 var URL = window.location.href;

 // Using path
 var URL = window.location.pathname;



answered 1 year ago Khaled Harby #13

You can get the full link of the current page through location.href and to get the link of the current controller, use:

location.href.substring(0, location.href.lastIndexOf('/'));

answered 1 year ago زياد #14;

answered 1 year ago tmh #15

Open Developer Tools, type in the following in the console and press Enter.


Ex: Below is the screenshot of the result on the current page.

enter image description here

Grab what you need from here. :)

answered 10 months ago Alireza #16

OK, getting the full URL of the current page is easy using pure JavaScript. For example, try this code on this page:

// use it in console of this page will return

The window.location.href property returns the URL of the current page.

document.getElementById("root").innerHTML = "The full URL of this page is:<br>" + window.location.href;
<!DOCTYPE html>

  <h3>The window.location.href</h3>
  <p id="root"></p>


Just not bad to mention these as well:

Also if you need a relative path, simply use window.location.pathname;

And if you'd like to get the host name, you can use window.location.hostname;

And if you need to get the protocol separately, simply do window.location.protocol

Also if you page has hash tag, you can get it like: window.location.hash

So window.locatation.href handles all in once... basically:

window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.hash === window.location.href;

Also using window no necessary if already in window scope...

So in that case you can use:






Get the current URL with JavaScript

answered 9 months ago Hitesh Sahu #17

Adding result for quick reference


 Location {href: "",
 ancestorOrigins: DOMStringList,
 origin: "",
 replace: ƒ, assign: ƒ, …}


  Location {href: "", 
ancestorOrigins: DOMStringList,
 origin: "",
 replace: ƒ, assign: ƒ
, …}







answered 8 months ago Hasib Kamal #18



answered 2 months ago Session Start #19

if you are referring to a specific link that has an id this code can help you.

                      var id = $(this).attr("id");

                          url: "<?php echo base_url('index.php/sample/page/"+id+"')?>",
                          type: "post",
                            alert("The Request has been Disapproved");

I am using ajax here to submit an id and redirect the page using window.location.replace. just add an attribute id="" as stated.

comments powered by Disqus