Jump to content

XHR Image loading - feature detection


alex_h
 Share

Recommended Posts

I'm trying to add the option to load images via XHR, with normal Image object loading as fallback. Since getting the XHR response into an image is dependent on window.URL.createObjectURL I'm using this for my feature detection to know when to use the XHR method:

 

window.URL = window.URL || window.webkitURL;var useXHR = true;if (!window.URL.createObjectURL) {    useXHR = false}

In testing it seems that this might not be working for some platforms though. Apparently in Safari on the iPhone 4S running iOS 5.1.1, and in the Android Stock browser on a Samsung S2 running Android 4.2.2 the loading fails. My best guess is that these browsers are attempting to use the XHR system when they shouldn't be, since older versions of this game that just used basic image loading worked fine on those devices.

Is there a better method I could be using to choose when to go for XHR over good old Image.src + Image.onLoad ?

 

I suppose the other thing that could be going wrong is there seem to be various different ways to listen for progress, load complete and load errors with the XHR model, some of which are legacy methods, some are ie specific, etc. I am currently assigning a load complete function to the XHR instance onload like so:

xhr2.onload = function () {   //do something                 };

Perhaps this method will only work in some browsers? In which case, if the onload doesn't fire then my game will hang after the first image successfully loads.

Any pointers and advice will be gratefully received!

Link to comment
Share on other sites

You are absolutely right Chris, after working on this for several hours last night I discovered that the problem was precisely that window.URL was not defined, so checking whether window.URL.createObjectURL existed was crashing the whole thing. Modifying the code to check whether window.URL existed fixed the issue on iOS, and I'm hoping it will now work on the Android devices too.

Thanks for your advice!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...