Unsparing
Unsparing

Reputation: 7653

Javascript: Document.URL.indexOf issue

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 ?

Upvotes: 0

Views: 1302

Answers (2)

mplungjan
mplungjan

Reputation: 177692

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

Upvotes: 0

putvande
putvande

Reputation: 15213

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...
}

Upvotes: 2

Related Questions