hi,
i want to know to how to upload image randomly with javascript page.Any recommendations and suggestions are appreciated.
hi,
i want to know to how to upload image randomly with javascript page.Any recommendations and suggestions are appreciated.
This is the code to upload image randomly with javascript.Code:<script language="JavaScript"> <! -- img = new Array () ran = Math.floor (4 * Math.random ()); img [0] = 'immagine1.jpg "width =" "height =" "border =""'; img [1] = 'immagine2.jpg "width =" "height =" "border =""'; img [2] = 'immagine3.jpg "width =" "height =" "border =""'; img [3] = 'immagine4.jpg "width =" "height =" "border =""'; document.write ( "<img src=\""+img[ran]+"\">"); / / -> </ script>
matrix array instantiated with a new type of variable. Although JavaScript does not have any type of data matrix, it is possible create a structure array defining an object array. By doing this, you can create properties for the object contains the data you need.img = new Array ()check a name to the matrix with the same operator = This operator assigns the variable the value of the left than the right. Every time you call the variable img will be referred to the matrix and properties of the data.
Now create the variable for the random function, to extract a given random in this case an image and access the individual properties of the matrix ran = Math.floor (4 * Math.random ()); The Math object provides constants and mathematical functions beyond those provided by standard operators for addition, subtraction, multiplication, etc. ...
in which each character represents the function to perform.The Math object is considered static, ie does not require an instance of to use the properties and methods of the object. There is no new statement to create a copy of Math. The method Math.floor (number) shows the nearest integer towards zero ES: Math.floor (32.5) becomes 32 you make a test with the examples below :
<script> document.write (Math.floor (32.5)) </ script>
If the decimal part of N and ° '0.5 ° N is rounded up 0 Same for negative numbers -24.8 becomes -25
<script> document.write (Math.floor (-24.8)) </ script>
After the method Math.floor between parentheses put the number of array elements this example the 4 and multiply them by using the method Math.random () the method Math.random () will extract a random from among those included in the elements of the matrix Construction matrix elements img [0] = 'immagine1.jpg "width =" "height =" "border =""'; Method: identifier [number] where the identifier is the variable that calls img matrix array, img [0] img [1] img [2] img [3] Nb. the first number assigned to the identification, inserted between the brackets must be zero [0]
if we start from a [1] img [1] img [2] img [3] img [4] this picture will never be displayed img [0] = 'immagine1.jpg "width =" "height =" "border =""'; if you want comfort or convenience first identifier assigned to the No. [1] nodificate variable ran like this: ran = Math.floor (4 * Math.random () +1); after the identifier just insert the images to be displayed Just img [0] = 'immagine1.jpg', but is recommended to enter the dimensions of the images to avoid (although slight) a slowdown in the loading of the browser having to perform this function This is the part that deals with the display of object document.write image document.write ( "<img src=\""+img[ran]+"\">"); we put the tag <img src =, the variable for the matrix img Array and between the brackets variable ran [ran] to the identifiers and the function random.
Bookmarks