i have a piece of jquery code that first checks device size when loading and for every subsequent windows resize it will put in the correct image. I found myself having to write the conditional statement twice. Is there a better way to achieve this? Thanks.
$(window).ready(function() {
var wi=$(window).width();
//upon loading for first time, show correct image size
if (wi <=420)
{
$("#main").find("img").attr("src","images/main_image_xs.jpg")
}
else
{
$("#main").find("img").attr("src","images/main_image_default.jpg")
}
}
$(window).resize(function() {
// When resizing windows, show corect image size
if (wi <=420)
{
$("#main").find("img").attr("src","images/main_image_xs.jpg")
}
else
{
$("#main").find("img").attr("src","images/main_image_default.jpg")
}
}
});
});
load(). Also you haven't calculated the new window size onresize()load, you usually wantready.readywill be fired once all of the page’s HTML has loaded and the DOM is complete;loadwaits until after all images, stylesheets, etc. have loaded as well, which could be significantly longer.document. There seems no connection of DOM and the window.