0

I have a javascript/jquery function that displays a notification. I want to make this function display a different mode if they aren't on x, y and z page.

This is how I tried to achieve this:

function display_alert(message, type, delay, mode)
{
    type    = (typeof type === "undefined") ? "danger" : type;
    delay   = (typeof delay === "undefined") ? 3000 : delay;
    mode    = (typeof mode === "undefined") ? 'normal' : mode;

    var current_location = document.URL;
    var home_locations = ['home', 'remote', 'zip'];

    for (var i = 0; i < home_locations.length; i++)
    {
        if (current_location.toString().indexOf(home_locations[i]) == -1)
        {
            // alert(home_locations[i]); return;

            mode = 'top';
            break;
        }   
    }

    ...

So if document.URL doesn't contain one of the array elements, then I want the mode variable to become top.

I think this is a simple problem, but I just can't see how to fix it.

2 Answers 2

1

You could use a regular expression built with your home_locations array :

var home_regex = new RegExp('('+home_locations.join('|')+')');
// home_regex = /(home|remote|zip)/;
if (!home_regex.test(document.URL)) mode = 'top';
Sign up to request clarification or add additional context in comments.

Comments

0

You need to reverse the logic in your loop...

var current_location = document.URL;
var home_locations = ['home', 'remote', 'zip'];

var found = false;

for (var i = 0; i < home_locations.length; i++)
{
    if (current_location.toString().indexOf(home_locations[i]) >= 0)
    {
        found = true;
        break;
    }   
}

if (!found) {
    mode = 'top';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.