Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get full anchor href? [duplicate]

Possible Duplicate:
How to get “raw” href contents in JavaScript

Sometimes you write in your code relative paths to your files, so in the code the href attribute value could be somefile.php yet when clicking of course the anchor would send you to http://www.yourdomain.com/somefiles.php

Now my question is could I somehow obtain the full href of an anchor?

When using $(anchor).attr("href") you only get the relative path.

like image 681
eric.itzhak Avatar asked Nov 19 '12 15:11

eric.itzhak


2 Answers

You can use .prop:

$(anchor).prop("href")

Here's an example:

console.log($("a").prop("href")); //http://fiddle.jshell.net/_display/sth
console.log($("a").attr("href")); //sth
like image 141
Matt Zeunert Avatar answered Oct 21 '22 05:10

Matt Zeunert


element.href will get the entire href

FIDDLE

EDIT:

I'll quote something from jQuery's website here:

The .prop() method should be used for boolean attributes/properties and for properties which do not exist in html (such as window.location). All other attributes (ones you can see in the html) can and should continue to be manipulated with the .attr() method.

Since .attr() gets the actual value typed in the attribute, as it probably used the native getAttribute(), the proper way to do this would be to get the native javascript element, and then use the native element.href which will get the href including the domain and pathname etc.

like image 20
adeneo Avatar answered Oct 21 '22 04:10

adeneo