views:

48

answers:

2

I can't get this to do what I want it to do! I'm trying to replace large images with smaller ones for mobile browsers.

Here is the code:

function js_imgs() {
    if (document.documentElement.clientWidth <= 600) {
        $("img").each(function(){
            if ($(this).hasClass('big')) {
                var preSrc = $(this).attr('src');
                var newSrc = preSrc.substring(preSrc.lastIndexOf('/'), preSrc.lastIndexOf('.'));
                $(this).attr('src', newSrc + '-m.' + /[^.]+$/.exec(preSrc));
                $(this).removeClass('big').addClass('mobile');
            }
        });
    } else {
        $("img").each(function(){
            if ($(this).hasClass('mobile')) {
                var preSrc = $(this).attr('src');
                var newSrc = preSrc.substring(preSrc.lastIndexOf('/'), preSrc.lastIndexOf('.'));
                $(this).attr('src', newSrc + '.' + /[^.]+$/.exec(preSrc));
                $(this).removeClass('mobile').addClass('big');
            }
        });
    }
};

For the first image, this code works swimmingly. Unfortunately, all the other images have their SRCs rewritten as the first image's SRC. They all turn into the same image; which resize wonderfully, but aren't the right image.

What have I done wrong? I've tried almost all of the variations on this in this thread with no success.

A: 

Firstly I would simplify the function, it is not worthy to verify if you are in mobile, because the first time you enter the page, it verifies if it is big enough or not.

It would be like this:

function js_imgs() {
    if (document.documentElement.clientWidth <= 600) {
        $("img").each(function(){
            if ($(this).hasClass('big')) {
                var preSrc = $(this).attr('src');
                var newSrc = preSrc.substring(preSrc.lastIndexOf('/'), preSrc.lastIndexOf('.'));
                $(this).attr('src', newSrc + '-m.' + /[^.]+$/.exec(preSrc));
                $(this).removeClass('big').addClass('mobile');
            }
        });
};

Notwithstanding, this work could be done better in serverside, there is a fantastic script in different server languages (asp.net, php...) to detect if it is a mobile:

detectmobilebrowser.com/

Another thing, I am not quite sure if the width (600) is too big, I woulld have use 350px, try facebook in mobile, it works for these dimensions.

netadictos
This doesn't fix the problem. The over-complexity comes from it being written to replace the images on the fly when resizing the window. This is not something I am certain I will keep, but I am toying with it anyway. I am using CSS3 media queries for the 'mobile' version of the site, so under 600px makes sense given the dimensions of the images and the fluid nature of the design.
Aleksandr
+1  A: 

Went away for a rest, then came back and discovered I had been testing my code by refreshing and refreshing the out-of-date copy on the server instead of the one I was editing at localhost.

Sorry! I have been working too long.

Here is a working version of the code:

function js_imgs() {
    if (document.documentElement.clientWidth <= 600) {
        $("img").each(function(){
            if ($(this).hasClass('big')) {
                var oldSrc = $(this).attr('src');
                var newSrc = oldSrc.substring(0, oldSrc.lastIndexOf('/')) + oldSrc.substring(oldSrc.lastIndexOf('/'), oldSrc.lastIndexOf('.'));
                $(this).attr('src', newSrc + '-m.' + /[^.]+$/.exec(oldSrc));
                $(this).removeClass('big').addClass('mobile');
            }
        });
    } else {
        $("img").each(function(){
            if ($(this).hasClass('mobile')) {
                var oldSrc = $(this).attr('src');
                var newSrc = oldSrc.substring(0, oldSrc.lastIndexOf('/')) + oldSrc.substring(oldSrc.lastIndexOf('/'), oldSrc.lastIndexOf('.'));
                $(this).attr('src', newSrc.replace(/([^.]*)-m/, "$1") + '.' + /[^.]+$/.exec(oldSrc));
                $(this).removeClass('mobile').addClass('big');
            }
        });
    }
};

js_imgs();

$(window).resize(function($){
    js_imgs();
});

This is written for on-the-fly switching on window resizes, which is almost certain to be entirely unnecessary. Perhaps when switching from portrait to landscape on high-resolution iPhones or tablets, the extra code might be worthwhile; I will have to do some testing.

Improvements and comments on the worthiness of on-the-fly switching more than welcome.

Aleksandr