Time for action – loading an image file
Let's add some code to the start()
method of our application to check if the File API is available. You can determine if a browser supports the File API by checking if the File
and FileReader
objects exist:
this.start = function() { // code not shown... if (window.File && window.FileReader) { $("#load-menu input[type=file]").change(function(e) { onLoadFile($(this)); }); } else { loadImage("images/default.jpg"); } }
First we check if the File
and FileReader
objects are available in the window
object. If so, we hook up a change event handler for the file input control to call the onLoadFile()
method passing in the <input>
element wrapped in a jQuery object. If the File API is not available we will just load a default image by calling loadImage()
, which we will write later.
Let's implement the onLoadFile()
event handler method:
function onLoadFile($input) { var file = $input...