0

Hello Guys I have a little issue with document.URL.indexOf in Javascript. I have a lounge page where I want to make the background transparent if there is "lounge" in the URL and I want to make the background different when I am viewing a single lounge but the URL still contains "lounge" cause the full link to a lounge is "lounge/view/1" How can I make it work so when I am in "view" to have the background I want and when I am in lounge to have it transparent.

This is the code I have for now:

if (document.URL.indexOf("lounge") > -1)
{
  body.delay(400).css('background-color', 'transparent');
}
else
{
  body.delay(400).css('background', '#b4b7b8');
}

I tried adding anothger "else if" saying if indexOF is "view" to go to the one I want but it didn't work.

Any suggestions ?

2 Answers 2

2

You can check both if lounge is in the URL and view is not:

if (document.URL.indexOf('lounge') > -1 && 
    document.URL.indexOf('view') == -1) {
    // your code...
}
Sign up to request clarification or add additional context in comments.

2 Comments

or just if (document.URL.indexOf('lounge/view') > -1)
As an elseif statement? Cause OP is asking for a background color when "lounge" is in URL and "view" is not.
0

How about

var color = document.URL.indexOf('lounge/view') > -1?'#b4b7b8':'transparent';
body.delay(400).css('background', color);

or another colour per view:

var color = 'transparent', url = document.URL;
var views = 'lounge/view';
if (url.indexOf(views) > -1) {
  var idx = parseInt(url.split(views)[1],10);
  color = ["",'#b4b7b8','#99b7b8','#eeb7b8'][idx]; // view/1, view/2, view/3
}
body.delay(400).css('background', color);

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.