# Randomizing the XML Gallery thing

**URL:** https://forum.kirupa.com/t/randomizing-the-xml-gallery-thing/193291
**Category:** flash
**Created:** [July 11, 2006, 7:00pm UTC](https://forum.kirupa.com/t/randomizing-the-xml-gallery-thing/193291 "2006-07-11T19:00:02Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![nine17com](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@nine17com](https://forum.kirupa.com/u/nine17com)
#### Post date: [July 11, 2006, 7:00pm UTC](https://forum.kirupa.com/t/randomizing-the-xml-gallery-thing/193291/1 "2006-07-11T19:00:02Z")

</div>

I used the tutorial for making a xml gallery and changed it up a bit to make a banner ad gallery from an xml file. This makes life easier for my non flash designers to just update the xml file when new ads come out. I would now like to make it randomly select the first image. Can anyone help. The only idea I had was to change the p = 0; part to p = Math.floor(Math.random() \* total); That didn’t work.

There is some additional code in there for a coldfusion click tracker and some to build the url string for the coldfusion page.

Here is the code: HELP!!

// Load XML, define arrays, and initiate loading of first image

function loadXML(loaded) {  
if (loaded) {  
xmlNode = this.firstChild;  
image = [];  
track = [];  
adName = [];  
destURL = [];  
adTracker = xmlNode.firstChild.nextSibling.attributes[“url”];  
total = xmlNode.firstChild.nextSibling.nextSibling.childNodes.length;

```
    for (i=0; i&lt;total; i++) {
        image* = xmlNode.childNodes[2].childNodes*.childNodes[0].firstChild.nodeValue;
        track* = xmlNode.childNodes[2].childNodes*.childNodes[3].firstChild.nodeValue;
        adName* = xmlNode.childNodes[2].childNodes*.childNodes[2].firstChild.nodeValue;
        destURL* = xmlNode.childNodes[2].childNodes*.childNodes[1].firstChild.nodeValue;
    }
   
firstImage();
} 

else {
    content = "file not loaded!";
}

```

}

xmlData = new XML();  
xmlData.ignoreWhite = true;  
xmlData.onLoad = loadXML;  
xmlData.load(\_root.xmlpath);

// Functionality for keyboard navigation

listen = new Object();  
listen.onKeyDown = function() {  
if (Key.getCode() == Key.LEFT) {  
prevImage();  
}

```
else if (Key.getCode() == Key.RIGHT) {
    nextImage();
}

```

};

Key.addListener(listen);

// Functionality for mouse navigation

previous\_btn.onRelease = function() {  
prevImage();  
};

next\_btn.onRelease = function() {  
nextImage();  
};

// Define p to zero to fix numbering issues

p = Math.floor(Math.random() \* total);  
// p = 0;

// Preloader for ads  
this.onEnterFrame = function() {  
filesize = adFrame.getBytesTotal();  
loaded = adFrame.getBytesLoaded();  
preloader.\_visible = true;

```
if (loaded != filesize) {
    preloader.preload_bar._xscale = 100*loaded/filesize;
}

else {
    preloader._visible = false;
    if (adFrame._alpha&lt;100) {
        adFrame._alpha += 10;
    }
}

```

};

// Next Image Function

function nextImage() {  
if (p\<(total-1)) {  
p++;  
if (loaded == filesize) {  
adFrame.\_alpha = 0;  
adFrame.loadMovie(image[p], 1);  
linkHolder.onRelease = function () {  
getURL (adTracker+"?t="+track[p]+"&n="+adName[p]+"&qs="+destURL[p]);  
};  
ads\_num();  
}  
}  
}

// Previous Image Function

function prevImage() {  
if (p\>0) {  
p–;  
adFrame.\_alpha = 0;  
adFrame.loadMovie(image[p], 1);  
linkHolder.onRelease = function () {  
getURL (adTracker+"?t="+track[p]+"&n="+adName[p]+"&qs="+destURL[p]);  
};  
ads\_num();  
}  
}

// Load first image automatically ADD RANDOM LOAD HERE?

function firstImage() {  
if (loaded == filesize) {  
adFrame.\_alpha = 0;  
adFrame.loadMovie(image[0], 1);  
linkHolder.onRelease = function () {  
getURL (adTracker+"?t="+track[0]+"&n="+adName[0]+"&qs="+destURL[0]);  
};  
ads\_num();  
}  
}

// Ad Numbering

function ads\_num() {  
current\_pos = p+1;  
}

[Here is a link](http://www.worshiphousemedia.com/sitecontent/advertising/mainads.xml) to the XML file if that helps.
