jquery if statements dependent on current url
I have a scenario where i want button colors to change depending on the page. I have the animation working with one default color and an array of other colors.
What i want to do though is when i am on the 'red' page to change the default(which is 'blue') to 'red'.
I know i can do this with if statements, but i want to compare it to the url. I dont know how to get the url and then store it. I know the exact pages that i can compare the url to, but i would also like it to work with sibling pages of that main.
I just dont know how to get the url with jquery, test against it, and test for siblings of the KNOWN urls.
EXAMPLE:
foobar.com开发者_StackOverflow社区 <- 'blue'
foobar.com/red/ <- 'red'
foobar.com/red/car <- still 'red'
foober.com/green/ <- 'green'
var urlRed = "foobar.com/red/"
var urlGreen ="foobar.com/green/"
var urlInput = ??
if(urlInput == urlRed){
...set default to red...
} else if( urlInput == urlGreen ){
...set default to green...
} else {
...set default to blue...
}
var url = window.location.pathname.split("/")[1];
switch(url){
case "red": // set default to red
break;
case "green": //set default to green;
break;
default: // set to blue (default)
}
$(function(){
var red = "red";
var green = "green";
var url = window.location.href.toLowerCase();
if(url.indexOf(red) >= 0){
...set default to red...
} else if( url.indexOf(green) >= 0 ){
...set default to green...
} else {
...set default to blue...
}
}
You can get the URL in Jquery using
$(document).ready(function() {
var pathname = window.location.pathname;
});
or
$(location).attr('href');
精彩评论