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
  • 11,157
  • 14
  • 47
  • 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é
  • 69,329
  • 26
  • 129
  • 166
0

Use

$(this).attr('href')

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

dotty
  • 40,405
  • 66
  • 150
  • 195