Micah
Micah

Reputation: 116150

Hyperlink Rewriting with Javascript/Jquery

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

Upvotes: 1

Views: 300

Answers (2)

artlung
artlung

Reputation: 33833

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/']")

Upvotes: 1

Joel
Joel

Reputation: 19368

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.

Upvotes: 3

Related Questions