views:

225

answers:

2

Is it possible o get the URI with javascript or is possible to break apart the href of the link and if so how, I am trying to run some ajax that has hover and click events and the method call for each ajax is the same so I need to be able get the unique ID that is passed in the URI.

A: 

HTML

<a href="/users/1/details">User</a>

jQuery

$("a").click(function(e){
   var href = $(this).attr('href'); // = /users/1/details

   //Once you have it, get the id:
   var id = href.split('/')[2]; // returns 1

   //Do something with it

   e.preventDefault(); // Keep the original link from being followed
}); 
Doug Neiner
+1  A: 

An anchor makes available the same properties that you find in window.location.

E.g.

<a id="mylink" href="http://website.com/page.html#content"&gt;Link&lt;/a&gt;

jQuery:

var anchor = $('a#mylink')[0]; // [0] to access DOM node

anchor.href; // => http://website.com/page.html#content
anchor.pathname; // => page.html
anchor.hash; // => #content
anchor.protocol; // => http:
J-P