Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Problem with Ajax and progressive JPEG

Featured Replies

I've created an AJAX lightbox but when loading progressive JPEGs sometimes the image is returned before it is fully rendered. I know this is the idea with progressive encoding but the problem is that when the server has returned the image it does not continue to load/render.

The response of the XMLHttpRequest object (the image wrapped in img tags) is added to my HTML on an onreadystatechange event and I am making sure that the readystate is = 4 and the status is = 200.

Is there anything I can do so the the response is only returned when the image is fully loaded? (without preloading all the images)

 

Code: (the funtion is a child of another function in case you wonder why som vars are not declared)

//This function passes a var (the image url) to a PHP script (the PHP script returns the image formatted in HTML img tags)
function handleAjax(e) {
    e.preventDefault();
    e.stopPropagation();
    var url = (this.href);
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            overlayInner.innerHTML = xmlhttp.responseText;
        }
        else {
            overlayInner.innerHTML = "Error: Content could not be loaded";
        }
    }
    var ajaxphp = 'ajaxhandler.php';
    xmlhttp.open("GET", ajaxphp + '?theurl=' + url, true);
    xmlhttp.send();
}

The PHP script just checks if the passed url is an image or a txt file, swf etc. and returns the file type in the appropriate markup and should not have anything to do with the issue but it looks like this:

//axaxhandler.php

$request = $_GET['theurl'];
$ext = pathinfo($request, PATHINFO_EXTENSION);

if ($ext=='jpg' || $ext=='png'){
    echo '<img src="'.$request.'" alt="image" >';
}
elseif($ext=='swf'){
    echo'
        <object type="application/x-shockwave-flash" data="'.$request.'" width="400" height="300">
        <param name="movie" value="'.$request.'">
        <param name="quality" value="high" />
        <param name="scale" value="exactFit" />
        <p>You need Flashplayer 10 to view the content on this page</p> 
		<a href="http://www.adobe.com/go/getflashplayer">
		    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
		</a>
    </object> 
    ';
}
else{
    echo '<div id="iframeContainer"><iframe src="'.$request.'" allowfullscreen></iframe></div>';
}

?>

Edited by Nillervision

I ajax in most of my images through a lazy loading script I wrote, and I don't get any issues with progressive JPGs as this is how I always save them. I am using jQuery for this though. I'd imagine jQuery's ajax functions fix this issue, I am 100% sure it's fixable in native JavaScript based on this but as for native javaScript to do cross browser ajax requests I prefer to stick with jQuery becuase it works everywhere and all the weirdness with different browser quirks are ironed out. It saves a lot of headaches IMO

 

Sorry I couldn't be of more help, but perhaps it's worth the extra few KB to use jQuery in this instance? :)

Edited by rbrtsmith

  • Author

I ajax in most of my images through a lazy loading script I wrote, and I don't get any issues with progressive JPGs as this is how I always save them. I am using jQuery for this though. I'd imagine jQuery's ajax functions fix this issue, I am 100% sure it's fixable in native JavaScript based on this but as for native javaScript to do cross browser ajax requests I prefer to stick with jQuery becuase it works everywhere and all the weirdness with different browser quirks are ironed out. It saves a lot of headaches IMO

 

Sorry I couldn't be of more help, but perhaps it's worth the extra few KB to use jQuery in this instance? :)

Yeah I've never had these problems when using jQuery either. But if its possible with jQuery it must be posible with native js also. If nothing else I could try to scan through the 10.000 lines of code in the jQuery and see if I can locate what the jQuery devs did :flm7:

  • Author

Yeah I've never had these problems when using jQuery either. But if its possible with jQuery it must be posible with native js also. If nothing else I could try to scan through the 10.000 lines of code in the jQuery and see if I can locate what the jQuery devs did :flm7:

Hmm.. no occurrences of the words "jpg","jpeg" or "progressive" in the jQuery library. All search results for "mime (type)" and "file (type)" seems to be refering only to txt, json, xml and html (the standard AJAX protocols)

Edited by Nillervision

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.