Image caching with html, JAVA Programming

Assignment Help:

In the airplane program, you may have noticed that the loading of each image appears to be jerky, erratic, or slow, and that the URL for each image flickers in the status bar each time the image changes. This happens because JavaScript does not save a copy of the image in memory that can be used whenever necessary. Instead, each time a different image is loaded by an element, JavaScript must open or reopen the image from its source. You probably accessed the airplane image files directly from the Data Disk on your local computer, and if you have a particularly fast computer, you may not have noticed a loading problem. If you did notice erratic loading of the images, then you can imagine how erratic and slow the animation would appear if you had to download the images from the Web server each time they are loaded. A technique for eliminating multiple downloads of the same file is called image caching. Image caching temporarily stores image files in memory on a local computer. This technique allows JavaScript to store and retrieve an image from memory rather than download the image each time it is needed.

Images are cached using the Image() constructor of the Image object. The Image() constructor creates a new Image object. There are three steps for caching an image in JavaScript:

1. Create a new object using the Image() constructor.

2. Assign a graphic file to the src property of the new Image object.

3. Assign the src property of the new Image object to the src property of an element.

In the following code, the src attribute of the element named myImage is initially set to an empty string "". In the script section, a new Image object named newImage is created. The newImage object is used to save and access the memory cache containing the image file. A file named graphic.jpg is assigned to the src property of the newImage object. The src property of the newImage object is then assigned to the src property of the element.

Be sure to understand that in the preceding code, the graphic.jpg file is not assigned directly to the src property of the element. Instead, the newImage object is assigned to the src property of the element. If you assigned the graphic.jpg file directly to the src property of the element using the statement document.myImage.src = "graphic.jpg";, then the file would reload from its source each time it was needed. The newImage object opens the file once and saves it to a memory cache.

The following code shows a version of the unicycle animation code modified to use image caching. The lines that add each image file to the unicycle[] array have been replaced by a for loop, which assigns a new object to each element of the unicycle[] array until the loadImages variable is greater than the length of the array. In the while loop, each object in the unicycle[] array is assigned an image file using the src property. In the turn() function, the unicycle[curUnicycle] operator in the statement document.images[0].src = unicycle [curUnicycle]; now includes the src property so that the statement reads document.images.


Related Discussions:- Image caching with html

Difference between boolean & operator and && operator, How can we define th...

How can we define the difference between the Boolean & operator and the && operator ?

Explain the phase compile- java program development, Explain the phase Comp...

Explain the phase Compile- Java Program Development In Phase 2, programmer gives the command javac to compile the program. Java compiler translates the java program into byte

Performs simple editsusing html and javascript, You are to write two versio...

You are to write two versions of this assignment. One using HTML and VBScript and one using HTML and Javascript. In both, you are to create an HTML webpage that: performs simple

Design a website in java using xml, The design of the website will be left ...

The design of the website will be left to the individual student. The website must be written only in xml, displayed in xhtml using transforms and xsl for styling elements. Javascr

Program for using arraylists in two ways, In this lab, you will practice us...

In this lab, you will practice using ArrayLists in two ways.  The first is in the problem domain class Question.java (see the UML diagram shown below) which contains data for trivi

Java web programming, Write a JSP to display a table that contains factoria...

Write a JSP to display a table that contains factorials for the numbers from 0 to 10,

Why uploading a web page is important, Why uploading a web page is importan...

Why uploading a web page is important? Describe in 2 - 3 lines? Through only uploading webpage we could place our data so that another can reach up to what we have loaded. Uplo

Multi treading array program, You are to write a program name arrayScaling....

You are to write a program name arrayScaling.java that will randomly generate 5000 integer number raging from 1 - 49 and place them in an array. 1.  The program will scale thi

Explain java is fully object oriented languages or not, No, java is not ful...

No, java is not fully object oriented language due to it does not support "multiple inheritance" and "pointers" which are used in C++. But, by using Interfaces we can execute multi

Array to store 10messages of type String., Ask1. Storing the Array: Write a...

Ask1. Storing the Array: Write an application that uses an Array to store 10messages of type String. You will store this Array with 10 messages of your choosing. For example, a mes

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd