What I’m trying to achieve:
Based on URL (ie., foo.com/item1), the div element “logoswap” receives a different class.
The following is the code I put together but it seems completely wrong. I’m not a JS pro by any means, XHTML/CSS is more my speed (some PHP)… I cannot use PHP, even if it is possible in PHP (and I know it is because I have a PHP version of what I need done already, but I can’t call the PHP properly.
I’m really just trying to get a different logo to show up based on the directory/url… It doesn’t have to be a background element called in by the CSS class necessarily, I just need a different image to load based on the aforementioned url variable…
$(function() {
var url = location.pathname;
if(url.indexOf('item1') > -1) {
document.getElementById("logoswap").className += " class1";
}
elseif(url.indexOf('item2') > -1) {
document.getElementById("logoswap").className += "class2";
}
elseif(url.indexOf('item3') > -1) {
document.getElementById("logoswap").className += "class3";
}
elseif(url.indexOf('item4') > -1) {
document.getElementById("logoswap").className += "class4";
}
elseif(url.indexOf('item5') > -1) {
document.getElementById("logoswap").className += "class5";
}
else {
document.getElementById("logoswap").className += "class1";
}
});
That’s what I have… Ugly I’m sure.
That’s why I’m here though, I definitely need some help.
Assigning CSS Class By URL Pathname
Here is a case for using numeric expressions if they are available. This does not apply to the above question.
UPDATE:
In light of the new details you may need an array of values, these should be derived from or exactly equal to the class names you intend to use.
To make this work you will need a few things in place. I will assume that the paths will end in a number as we have outlined here. The default ends with
1. You will need the images to be accessible. You need to define the styles for each possibility.CSS Setup
Without jQuery
if you do not have jQuery in your code you may need to use
window.onload.