Categories
javascript jquery url

Get current URL with jQuery?

2003

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

Example URL:

http://localhost/menuname.de?foo=bar&number=0

4

  • 1

    you can see also tech-blog.maddyzone.com/javascript/…

    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: bugs.jquery.com/ticket/7858

    – feeela

    Jul 15, 2015 at 14:05


2740

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 (https://example.com/path/example.html)
var origin   = window.location.origin;   // Returns base URL (https://example.com)

12

  • 82

    Properties of the location object: developer.mozilla.org/en/DOM/window.location

    – 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 “mail.google.com/mail/u/0/#mbox/13005b79fe72f448” 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

837

In pure jQuery style:

$(location).attr('href');

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

6

  • 56

    Apparently, using $(location) in jQuery is unsupported and advised against: bugs.jquery.com/ticket/7858

    – 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 bugs.jquery.com/ticket/7858#comment:4

    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

502

http://www.refulz.com:8082/index.php#tab2?foo=789

Property    Result
------------------------------------------
host        www.refulz.com:8082
hostname    www.refulz.com
port        8082
protocol    http:
pathname    index.php
href        http://www.refulz.com:8082/index.php#tab2
hash        #tab2
search      ?foo=789

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

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

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script>
  $(location).attr('href');      // http://www.refulz.com:8082/index.php#tab2
  $(location).attr('pathname');  // index.php
</script>
</html>

4