I'm using 'mbanner' to rotate Flash and animated gif ad banners on my website's homepage. It works great! However; my boss came to me to say that most of the advertisers whose banners are up are mad because the same banner always appears first. So to be fair, I need to make the banner ads appear in random order, making sure that a different banner appears whenever the visitor returns to the homepage. I found this banner script by Manish Kumar Namdeo, but I cannot locate the author to ask how to randomize the banners. Below is the javascript and below that is the piece of code on the homepage that works with the javascript. If anyone can help me edit the javascript so as to make the banners appear in random order, I would really appreciate it. Thanks in advance! Anessa.........
This will show banners completely randomly which means any banner may come up any number of times. It atleast makes sure it's not displaying the same banner twice in a row. I could extend it further by giving it a "memory" of which banners have previosly been shown, so it won't display one banner twice before all other banners have been shown.
Note: I haven't tested the code, there might be errors.
The first problem was random number loop. It was supposed to give you yet another random number if the first one
turned out to be the same as the old one. (The numbers are used as indexes which points to which banner should be displayed)
Instead it did the opposite and gave you a new number until it was the same as the old one. (Hence showing the same banner all the time)
The second problem was that instead of setting this.currentBanner to the random number, I used the number as the index in the array containing the banners (this.aNodes). Hence it set the variable to the actual banner object instead of the number.
These two combined to create an infinite loop which makes the browser crawl.
Since an object can never equal the number stored in oldBanner, the loop will continue forever.
Told you it wasn't tested...
To implement correct back and forward button support, I'd need to store the order in which the previus banners were shown,
you also need to allow the function to take an argument (say 1 and -1) to show which direction to step in. Then just call that function with the correct argument and you've got it.
Changing the effect is quite a bit harder since some of the banners might be flash banners...
Here's the fixed code.
How can i make it so that there would be a standard file that would be loaded each time FIRST and after it's done, all other files would be randomly imported?
Here's the complete code as it is now for clarity.
I'll have to think about how to make it load a default banner before the others, I'm assuming you want the page to load a bit faster by only loading one banner when the page first loads. Then you start loading the other banners in the background and begin the rotation when it's done.
If you don't care about preloading and just want to make sure a certain banner is always displayed first, I've got a simple solution already.
I modified the str string a bit, now it only makes a few appends instead of having to do it many times.
if (prevBanner >= 0){
document.getElementById(this.aNodes[prevBanner].name).style.display="none";
}
document.getElementById(this.aNodes[thisBanner].name).style.display="block";
Now I'm trying to do the same thing with your modified code but it doesn't work.
Instead of:
Code:
if (document.getElementById(this.aNodes[oldBanner].name).className=='m_banner_show'){
document.getElementById(this.aNodes[oldBanner].name).className = "m_banner_hide";
}
document.getElementById(this.aNodes[this.currentBanner].name).className = "m_banner_show";
I'm trying to use:
Code:
if (document.getElementById(this.aNodes[oldBanner].name).style.display=='block'){
document.getElementById(this.aNodes[oldBanner].name).style.display="none";
}
document.getElementById(this.aNodes[this.currentBanner].name).style.display="block";
Please try to help me to solve this problem. Thanks!
Do you have a testpage online where the problem occurrs?
In which browsers does this occurr?
I can't see anything wrong with the modifications you've made. But try this, depending on what the page looks like it might cause problems if you set display to "block". Therefore I switched it around to check for "none" and set display to an empty string. When it's empty, a stylesheet should take over (since inlined styles have priority over other stylesheets when they are set). Since you don't have any stylesheets the default display value for that element should end up in there, making it visible.
It's just a guess tho...
Code:
if (document.getElementById(this.aNodes[oldBanner].name).style.display!='none'){
document.getElementById(this.aNodes[oldBanner].name).style.display="none";
}
document.getElementById(this.aNodes[this.currentBanner].name).style.display="";