0

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

3 Answers 3

2
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

Sign up to request clarification or add additional context in comments.

2 Comments

why the star doesn't display. The regex should be ".*profile&(\d+)&?".
The * doesn't display because they're used by Markdown for formatting. There's a big orange question mark icon in the editor that will lead you to some editing help.
1

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

Comments

0

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.