views:

210

answers:

2

Here's what I"m trying to do. I have a hyperlink like this on my page:

<a href="http://www.wikipedia.com/wiki/Whatever_Page_Here"&gt;Whatever Page Here</a>

When a user clicks the link I want to grab that click. If it's a link I don't want them to click (like something that doesn't match the format http://www.wikipedia.com/wiki/xxxxx) I want to pop a message box. Otherwise I want to funnel them through my Mvc Controller instead. like this:

//Category is the "Whatever_Page_Here" portion of the Url that was clicked.
public ActionResult ContinuePuzzle(string category)
{

}

Any suggestions?

+3  A: 

Start by intercepting all the click events:

$(function() {
    $("a").click(ClickInterceptor);
});

function ClickInterceptor(e)
{
    // code to display popup or direct to a different page
    if (redirect)
    {
        var href = $(this).attr("href").split("/");
        location.href = "http://mydomain.com/controller/" + href[href.length - 1];
    }

    return false;
}

The "return false;" tells the anchor not to fire the navigate.

Joel Potter
Awesome! That's exactly what I need. How can redirect them though?
Micah
FYI, this.href should work the same as $(this).attr('href')
bendewey
True enough, but since we're already using jquery. ;)
Joel Potter
+1  A: 

If you want to select all the a tags which do not start with "http://http://www.wikipedia.com/wiki/" you would use:

$("a").not("a[href^='http://http://www.wikipedia.com/wiki/']")
artlung