views:

137

answers:

2

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 visits the main splash page and as long as they have not been redirected there by the site itself (via timeout on 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
    }

}
A: 

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 :)

jaywon
I don't want to do it via Cookies because alot of our users clear those out regularly, and I kinda don't want to use a query string, but instead just check the host name of the referring site and see if it matches the sites hostname then don't show the modal, if it is different than show the modal, make any sense(or possible)?
Seth Duncan
I wasn't aware of this previously, but it looks like you can grab the referer property in Javascript via document.referrer, although I'm not sure if that call is supported in all browsers.
jaywon
A: 

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

jitter
Thank you very much perfect
Seth Duncan