I don't know you browser support specification but there is HTML5 approach: <picture>
element. But there are few browsers that it support - http://caniuse.com/#feat=picture
UPDATE
Another approach - use CSS and @media
rules. But here you will deal with background images instead of <img>
elements that may not fit with semantics (When to use IMG vs. CSS background-image?):
@media screen and (max-width: 480px) {
div.my-image {
background-image: url(files/design/logo_480.jpg);
}
}
@media screen and (max-width: 768px) {
div.my-image {
background-image: url(files/design/logo_768.jpg);
}
}
@media screen and (min-width: 768px) {
div.my-image {
background-image: url(files/design/logo_960.jpg);
}
}
Browser support - IE9+, Chrome 21+, FF 3.5+ (http://www.w3schools.com/cssref/css3_pr_mediaquery.asp)
UPDATE #2
Another approach - instead of write
content you can simply change src
of existing <img>
.
var w=window.innerWidth;
var img = document.getElementById("logo");
if (w<=480){
img.src = "files/design/logo_480.jpg"
}else if(w<=768){
img.src = "files/design/logo_768.jpg"
}else{
img.src = "files/design/logo_960.jpg"
}