javascript jquery url

Get current URL with jQuery?


I am using jQuery. How do I get the path of the current URL and assign it to a variable?

Example URL:



  • 1

    you can see also…

    Dec 12, 2013 at 11:42

  • 4

    I think the question should be restored to asking for jQuery, since there is an answer for that, regardless of whether jQuery is required to accomplish the task.

    – goodeye

    Jun 3, 2014 at 1:25

  • 1

    possible duplicate of Get current URL with JavaScript?

    – vimal1083

    Jul 16, 2014 at 10:50

  • 3

    @goodeye No, there is no jQuery way to get the location; as of the jQuery bug tracker: »It may have worked but it was never supported or documented. Simply use document.location.href which is faster, simpler, and easier to understand.« In other words, some folks used jQuery to get the location, but they relied on a bug, rather than feature. See:

    – feeela

    Jul 15, 2015 at 14:05


To get the path, you can use:

var pathname = window.location.pathname; // Returns path only (/path/example.html)
var url      = window.location.href;     // Returns full URL (
var origin   = window.location.origin;   // Returns base URL (


  • 82

    Properties of the location object:

    – bentford

    May 6, 2010 at 23:28

  • 102

    Far from killing it, jQuery’s given Javascript a new life. Do new C#/Java programmers understand pointers? No. Do they need to? Not really, newer abstractions are powerful enough for it not to matter..

    – flesh

    Jan 11, 2011 at 22:10

  • 203

    “How do I XYZ in jQuery” and the answer being plain javascript is pretty common. You may know how to do something in plain javascript; however, due to browser inconsistencies you may prefer to do it the “jQuery” way. I remember pre-jQuery or framework I would first check browser and then do what I wanted a handful of ways. So is jQuery killing plain js… yes, thank god, but it is also making it usable.

    – Parris

    Jan 20, 2011 at 21:14

  • 9

    this doesn’t work for the full url. for example. for “” this will only return /mail/u/0

    – dwaynemac

    May 19, 2011 at 18:53

  • 12

    Ummm,… window.location.pathname only gets the URL up the “?” and doesn’t get the query params as asked in the question.

    Dec 28, 2012 at 19:05


In pure jQuery style:


The location object also has other properties, like host, hash, protocol, and pathname.


  • 56

    Apparently, using $(location) in jQuery is unsupported and advised against:

    – Peter

    Aug 24, 2011 at 14:41

  • 10

    @Peter Bug closed as invalid.

    – kevinji

    Dec 19, 2011 at 19:40

  • 22

    @mc10: The “invalid” part applies to the request to support $(location); this should NOT be used.

    – Peter

    Dec 30, 2011 at 10:31

  • 6

    This answer is not needed, and question and answer can be updated to not use jquery. Reasons can be found here

    Jan 19, 2012 at 5:31

  • 8

    @HaralanDobrev: You shouldn’t be able to do .attr() on location. (1) It’s not an element, so $(location) is shady at best, and (2) even if it worked, you should be using .prop() to get properties. .attr() is for HTML attributes.

    – cHao

    May 20, 2013 at 6:09


Property    Result
port        8082
protocol    http:
pathname    index.php
hash        #tab2
search      ?foo=789

var x = $(location).attr('<property>');

This will work only if you have jQuery. For example:

<script src=""></script>
  $(location).attr('href');      //
  $(location).attr('pathname');  // index.php