Reputation: 393
I'm doing a hovercard plugin for my site but have a problem with getting user id.
profile urls can be;
hxxp://mysite.com/?p=profile&id=1
hxxp://mysite.com/?p=profile&id=1&page=2
hxxp://mysite.com/?p=profile&id=1&v=wall
etc..
How can I get profiles' id by javascript Regexp Replace?
$(document).ready(function () {
var timer;
$('a[href*="hxxp://mysite.com/?p=profile"]').hover(
function () {
if(timer) {
clearTimeout(timer);
timer = null
}
timer = setTimeout(function() {
// profile_id
// and get id's hovercard content here
},1000);
},
function () {
if(timer) {
clearTimeout(timer);
timer = null
}
$('.HovercardOverlay').empty();
}
);
});
Upvotes: 0
Views: 334
Reputation: 78560
I would do it like this:
var url = "hxxp://mysite.com/?p=profile&id=1&v=wall"; // this.href or w/e
var paramsArray = url.match("[?].*")[0].substr(1).split("&");
var params = {};
for (var i in paramsArray)
{
var parts = paramsArray[i].split("=");
params[parts[0]] = parts[1];
}
Then to get the id it's as simple as params.id
Upvotes: 1
Reputation: 3511
var result = $(this).attr("href").match(".*profile&id=(\d+)&?.*")
var id = result[1]
This has been tested on http://www.regular-expressions.info/javascriptexample.html
Upvotes: 2
Reputation: 10772
Here's how you can get the URL for the current link:
$('a[href*="hxxp://mysite.com/?p=profile"]').hover(
function () {
if(timer) {
clearTimeout(timer);
timer = null
}
timer = setTimeout(function() {
// profile_id
var myUrl = $(this).attr("href");
// and get id's hovercard content here
},1000);
},
And then to match the regular expression:
> var myUrl = "hxxp://mysite.com/?p=profile&id=5";
> var pattern = new RegExp("id=([0-9]+)");
> pattern.exec(myUrl);
["id=5", "5"]
Upvotes: 0