I have encountered an issue with my JavaScript. Sometimes, the code doesn't execute at all and i can't corelate this to a specific thing, like throwing an error and halting the code or something similar. What i intend to do is to change the picture (a logo) depending of the resolution of the device.
function logo_change() {
if ( document.body.clientWidth < 390 ) {
document.getElementById('logo').style.display = 'none';
document.getElementById('logo-m').style.display = 'block';
}
else {
document.getElementById('logo-m').style.display = 'none';
document.getElementById('logo').style.display = 'block';
}
}
window.onresize = function() {
logo_change();
};
window.onload = function() {
logo_change();
};
#logo {
max-width: 20%;
min-width: 185px;
float: left;
margin-top: 17px;
}
#logo-m {
width: 50px;
float: left;
margin-top: 17px;
display: none;
}
<a id='logo-l' class="logo-l" href="index.php">
<img src="https://d1afx9quaogywf.cloudfront.net/sites/default/files/Logos/facebook-logo_0.png" id='logo' class="logo" name="logo" height="60"></img>
<img src="https://www.ricksdailytips.com/wp-content/uploads/2016/08/f.png" id='logo-m' class="logo-m" height="50"></img>
</a>
I have tried of doing this with media queries doing basically the same thing( with display block and none), but the link would collapse and such.
Also, if i resize the page, the code executes and the logo changes. Thus, i thing is is a window.onload
thing.
Thanks for your responses.