i’m just starting to learn jquery and i wanted to know if i’m doing it right.
here is my code
var images = ['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg','6.jpg', '7.jpg', '8.jpg', '9.jpg'];
if (window.location == 'http://localhost:81/ci/login') {
$('body').css('background-image', 'url("public/images/login_4.jpg")');
} else if(window.location == 'http://localhost:81/ci/index.php/login') {
$('body').css('background-image', 'url("../public/images/login_4.jpg")');
}else {
$('body').css({'background-image': 'url(public/images/' + images[Math.floor(Math.random() * images.length)] + ')'});
}
what the first block does is check if the location is the same as the given string
the problem with that is that if the url looks like this
http://localhost:81/ci/login? or http://localhost:81/ci/login#
the code fails… I know there is a regex solution for that. What do you think fellas?
You don’t need any regex for this, you can just use
indexOf()Or better yet, combine them into this for more portable code: