Udders
Udders

Reputation: 6986

URI and jQuery help

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.

Upvotes: 0

Views: 1805

Answers (2)

Doug Neiner
Doug Neiner

Reputation: 66201

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
}); 

Upvotes: 0

James
James

Reputation: 111960

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">Link</a>

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:

Upvotes: 1

Related Questions