Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi experts..

I'm developing a web using aspx. My web is based on gallery uploading. So i need something like on facebook photo upload. When i upload a folder that contain the image it will automatically create a div with the album name on my page.

below is the code that i used to insert image on div.

JavaScript
//CREATE DIV BOX
   var dvs = document.createElement('div');
   dvs.style.position = "absolute";
   dvs.style.top = Vtop + "px";
   dvs.style.left = Hleft + "px";
   dvs.style.height = "120px"
   dvs.style.width = "100px";
   dvs.style.cursor = "pointer";
   dvs.onclick = function(){CreateFrame(DivID)} ;
   dvs.setAttribute('id',DivID);

JavaScript
//CREATE IMAGES & EMBED IN DIV
   var EbdImg = document.createElement('img');
   EbdImg.setAttribute('src','Web_Images/arrow_down.png');
   EbdImg.setAttribute('width','100%');
   EbdImg.setAttribute('height','100px');
   EbdImg.style.width = "100%"
   dvs.appendChild(EbdImg);


But the problem is, i need to insert random image automatically to the album from the latest folder that i upload and not from the existing folder from directory.
thanks in advance :)
Posted
Updated 29-Jan-13 23:26pm
v2

Assuming you have the list of images. Code goes like this -
JavaScript
var imgPath    = 'Web_Images/',
    imageArray = ['img01.jpg', 'img02.jpg', 'img03.jpg', 'xyz.jpg', 'abc.jpg'];

function getRandomImg(s) {
  var selectedImg, randomImgIndex;
  randomImgIndex= Math.floor(Math.random(imageArray.length)*imageArray.length);
  selectedImg = imageArray.splice(randomImgIndex,1);
  return selectedImg;
}

function createImages() {
  var selectedImg, EbdImg;
  while(imageArray.length != 0) {
    selectedImg = getRandomImg();

    //modify it according to your need
    EbdImg = document.createElement('img');
    EbdImg.src = imgPath + selectedImg;
  }
}

createImages();
 
Share this answer
 
Hi Expert,

I have solve the problem. Thanks a lot for the solution that u posted, it really helps me to solve the problem that i faced. After some adjustment i made below is the code i manage to produce.

var imgPath = "";
var imageArray = ['t_0001.jpg', 't_0002.jpg', 't_0003.jpg'];

imgPath = 'Gallery/' + DivID + '/thumbs/';

var selectedImg, EbdImg;
while(imageArray.length != 0)
{
selectedImg = getRandomImg();
}
var EbdImg = document.createElement('img');
EbdImg.setAttribute('src', imgPath + selectedImg);
EbdImg.setAttribute('width','100%');
EbdImg.setAttribute('height','100%');
EbdImg.style.width = "100%"
EbdImg.style.borderRadius = "1px";
dvs.appendChild(EbdImg);

imageArray = ['t_0001.jpg', 't_0002.jpg', 't_0003.jpg'];

function getRandomImg(s)
{
var selectedImg, randomImgIndex;
randomImgIndex= Math.floor(Math.random(imageArray.length)*imageArray.length);
selectedImg = imageArray.splice(randomImgIndex,1);
return selectedImg;
}

This is my Output
www.kishamegaevents.com/WebGallery.aspx
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900