简体   繁体   中英

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?

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.

如果您想选择所有不以“ http:// http://www.wikipedia.com/wiki/ ”开头的标签,您可以使用:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM