基于自定义功能结果的触发功能参考URL

时间:2009-12-17 00:21:56

标签: javascript jquery url

如果我的函数的结果为true且引用的URL不属于域

,我需要使用javascript(jquery,如果适用)来触发我的模态调用

希望用户访问主要的启动页面,只要它们没有被站点本身重定向(通过会话超时,无效的登录凭据等),它就会显示消息:

function showModalIf()
{
    if (checkFunction)
    {
        if(////// REFERRING URL not from this site)
              Trigger Modal Call

        else
              Don't Do anything else
    }

}

2 个答案:

答案 0 :(得分:0)

如果您在谈论代码中的强制重定向,而不仅仅是从站点的其他位置单击超链接,则可以在重定向上添加查询字符串参数并检查该方式。另一种选择是设置cookie并在javascript中检查cookie。

以下是Javascript中cookie处理的一个很好的链接:

Javascript - Cookies

这里也是一个用于在Javascript中解析查询字符串参数/哈希的方法:

Parsing The Querystring with Javascript

希望这能指出你正确的方向:)

答案 1 :(得分:0)

假设您使用jQuery UI Dialog来显示模态

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');
    }
}

检查演示页面http://jsbin.com/efico