Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

Search:

CA216      CA249      CA318

CA400      CA651      CA668


Lab - Javascript



Lab - Random image each time page is loaded




Exercise - Re-code random logo using Array

Re-code the random logo using an Array of image URL strings:

 var thearray = [ 
	"http://humphrysfamilytree.com/Icons/logo.rotate.2.jpg", 
	"http://upload.wikimedia.org/wikipedia/commons/5/5d/Mona_Lisa_Portrait.png",
	"http://computing.dcu.ie/~humphrys/3d/Images/earth.1.big.jpg"
	];

 var i = Math.floor ( Math.random() * thearray.length );	// returns random 0 to (thearray.length - 1)	 

 var image = ....   // get the ith element of the array

 ....   // display the image inline      

Advantages:
  1. Can have any filenames. No need for careful naming.
  2. No MAX needed.
  3. Can add files by adding line to Array. No adjustment to MAX needed.
  4. Can remove files by removing line from Array. No re-naming/re-numbering needed. No adjustment to MAX needed.
  


Lab - Mouse over changes image




Test - infinite loop

Remote site sending you code to execute is a strange idea.

What happens if a remote site sends you an infinite loop? Does it crash your browser?

Try this:


        <script>
            while ( true ) { }
        </script>

Or this:


        <script>
            var i = 1;
            while ( i > 0 ) { }
        </script>

  

Loops in Javascript




Feeds      w2mind.org

On Internet since 1987.