I have some JQuery written which is intended so when a user navigates to ‘page/2/’ in WordPress, an image appears in the sidebar. It’s CSS is set to display:none; initially, then I have JQuery change it to display:block; if that URL string is present. The code I wrote has it backwards… so it does this to every page BUT ‘/page/2/’.
$(document).ready(function() {
var url = window.location.pathname;
if(url.indexOf('/page/2/')){
$('.sidebarimage').css("display","block");
}});
I always thought if there was no condition set in an If statement, it treats it as true, and anything else as false. Should I put a = 1 or something here? What am I missing?
Many thanks SO
The
ifblock will be entered if the value of the condition is convertible totrue. In Javascript any non-zero number istrueand the return value ofindexOfwhen the string is not found is -1. Hence any time it’s not there it gives back -1 which is seen as true and theifblock is entered.Change the conditional to be explicit to what you’re looking for