meiryo
meiryo

Reputation: 11697

Getting pathnames (relative to host) from <a> with href="../foo/"

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?

Upvotes: 1

Views: 1303

Answers (2)

Fabr&#237;cio Matt&#233;
Fabr&#237;cio Matt&#233;

Reputation: 70199

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.

Upvotes: 3

dotty
dotty

Reputation: 41523

Use

$(this).attr('href')

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

Upvotes: 0

Related Questions