开发者

Trigger function based on result of custom function Referring URL

I need to use JavaScript (jQuery if applicable) to trigger my modal call if the result of my function is true and the referring URL is not of the domain.

The desire is that the user vi开发者_StackOverflow中文版sits the main splash page and as long as they have not been redirected there by the site itself (via timeout on a session, invalid login credentials, etc) it displays the message so:

function showModalIf() {
    if (checkFunction) {
        if(////// REFERRING URL not from this site)
            Trigger Modal Call
        else
           Don't Do anything else
    }
}


Assuming you use jQuery UI Dialog to show the modal

function checkReferrerExternal() {
    if (!document.referrer || document.referrer == '') return false;
    var regex = /^https?:\/\/\/?([^?:\/\s]+).*/;

    var referrermatch = regex.exec(document.referrer);
    var locationmatch = regex.exec(document.location);
    return referrermatch[1] != locationmatch[1];
}


function showModalIf() {
    if (checkReferrerExternal()) {
        //show jQuery UI Dialog modal or replace with whatever
        $("div#dialog").dialog('open');
    }
}

Check demo page http://jsbin.com/efico


If you are talking about forced redirection in the code, and not just a hyperlink click from elsewhere in the site, you could add a query string parameter on your redirection and check that way. Another option is to set a cookie and check for the cookie in javascript.

Here is a nice link on cookie handling in Javascript:

Javascript - Cookies

And here's one for parsing query string params/hashes in Javascript as well:

Parsing The Querystring with Javascript

Hope this points you in the right direction :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜