How to get full href binding?

Possible duplicate:
How to get raw href content in JavaScript

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

Now my question is: is it possible to somehow get a full href binding?

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

+4
source share
3 answers

element.href will get the whole href

Fiddle

EDIT:

I will quote something from the jQuery site here:

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

Since .attr() gets the actual value entered in the attribute, since it probably used its own getAttribute() , the right way to do this would be to get your own javascript element and then use native element.href , which will get href, including domain and path etc.

+11
source

You can use . prop :

 $(anchor).prop("href") 

Here is an example :

 console.log($("a").prop("href")); //http://fiddle.jshell.net/_display/sth console.log($("a").attr("href")); //sth 
+10
source

you can use jQuery prop function.

for testing, try the following codes in the console for this page;

 $("a:eq(6)").prop("href") $("a:eq(6)").attr("href") 

and for more information you can find the html attribute and property.

0
source

Source: https://habr.com/ru/post/1446991/


All Articles