I want a logo image to responsively change between small and large. Here's my code:
v.checkIfMobile = function() {
var newIsMobile = window.matchMedia("(max-width: 60em)").matches;
var valueToReturn = (v.isMobile != newIsMobile);
v.isMobile = newIsMobile;
return valueToReturn;
}
v.responsiveAdjust = function() {
console.log("will now check mobile status");
if(v.checkIfMobile()) {
console.log("mobile status changed");
var src = "local/images/logo"
+ (v.isMobile ? "Mobile" : "Desktop") + ".png";
$("#logo").attr("src", src);
console.log("src should have changed");
}
}
v.responsiveAdjust();
$(window).on("orientationchange", v.responsiveAdjust);
$(window).resize(v.responsiveAdjust);
Almost every time, the source simply doesn't change, but once or twice reloading the page did it. I've put a breakpoint right after the command and it made no difference. I've used both prop and attr, and I know the code is being processed because of the console messages.
Finally, when I enter the relevent code into the console after the page has loaded, it works as expected. Could there be a race condition? It's weird because the actual text of the src attribute doesn't change (until I do the console entry or unless I'm lucky) which doesn't sound like an effect of the time it takes to load the image.
What should I do?