1

The following code will return as undefined:

$('a').each(function () {
    console.log($(this).pathname);
});

My anchors look like this:
<a href="../foo/">Foo</a>

What am I doing wrong? If it is not possible then how can I return the full url?

meiryo
  • 10,393
  • 12
  • 45
  • 52

2 Answers2

3

In HTML5 browsers you can use this.pathname:

$('a').each(function () {
    console.log(this.pathname);
});

Fiddle

pathname is a property of the Anchor element, not of a jQuery object.

Edit: The anchor's pathname property has been standardized in HTML5, but even IE6 supports it natively.

Fabrício Matté
  • 67,789
  • 24
  • 124
  • 163
0

Use

$(this).attr('href')

The .attr() method extracts an attribute. You can see the docs at http://api.jquery.com/attr/

dotty
  • 38,389
  • 65
  • 144
  • 195