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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With