Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Rotating Iframe

Status
Not open for further replies.

elitehipster

Technical User
Feb 6, 2009
5
0
0
I have this rotating iframe script. Here is my problem, this script rotates the iframes in random. id like it to rotate the iframes in order. i have no idea about javascript but im thinking it would be as easy as changing "random" to a diff word maybe. any help would be great, thanks!



<script language="javascript">
/* <![CDATA[ */
/*


var iframeAds = new Array(
'iframe code'
'iframe code'
'iframe code'

);

var randValue = Math.round( Math.random() * ( iframeAds.length - 1 ) );

document.write( iframeAds[randValue] );

/* ]]> */
</script>
 
Hi,

Your script above doesn't "rotate", it simply picks a random array element then writes it to the screen. In order to rotate you would need to make use of setTimeout etc.

However, if you would like to loop through each array element 1 at a time in order then you would want to make use of a for loop...

Code:
var iframeAds = new Array(
    'iframe code A',
    'iframe code B',
    'iframe code C'
);

for (value = 0; value < iframeAds.length; value++){
	document.write( iframeAds[value] );
}

And here is an untested method in which you could produce a rotation effect.

Code:
rotate(0);
function rotate(val){
	document.write(iFrameAds[val]);
	if(val < srcs.length){val++;}
	else{val=0;}
	setTimeout("rotate("+val+")",1000);
}

Chris
 
sorry i really dont know the right wording to use. and what you said is pretty beyond me. but that you soooo much for responding! ive posted this on so many forums and no one has answered, cept you :)

ok, this is what i wanna do. everytime someone views the page or refreshes then it changes the iframe in order....

the original code i posted works fine for that, except its not even. im sure it would end up even out of 100s of page views, but my site has little traffic so i wanted it to be equal, thus changing in order.

anyway, thanks im going to try both right now and ill post back.
 
ok, i tried this one

var iframeAds = new Array(
'iframe code A',
'iframe code B',
);

for (value = 0; value < iframeAds.length; value++){
document.write( iframeAds[value] );
}


and its not working. I tried it by switching out "value" with 0 and it only showed the 1st one, then i tried with value as 1 and it only showed the second. then i did 2 and it showed none (i only had 2 iframe code)

so what am i doing wrong? i really know very little about javascript so specifically what i cut and paste in my editor would be great if you can.
here is what i have right now....

<script language="javascript">
/* <![CDATA[ */

var iframeAds = new Array(
'<iframe src=" align="middle" width="470" height="497" frameborder="0" scrolling="no"></iframe>',
'<iframe src=" align="middle" width="470" height="497" frameborder="0" scrolling="no"></iframe>'
);

for (value = 0; value < iframeAds.length; value++){
document.write( iframeAds[2] );
}
/* ]]> */
</script>
 
Hi,

If you would like the user to see a different advert each visit to the page (in a particular order) then you would probably have to store which advert (array element number) was last displayed in the users browser cookies. Then you would add 1 to that number or return it to 0 if it has reached the last advert (to re-loop again).

Also...
Code:
 '<iframe src="[URL unfurl="true"]http://www.yahoo.com"[/URL] align="middle" width="470" height="497" frameborder="0" scrolling="no"></iframe>',

I see alot of repeated attributes etc. It would be best to put the iframe html into the body of your page and use javascript to change the src of the iframe. That way you only need to have an array of urls instead of the full iframe html code.

I'm a bit busy at this time but ill write an example this evening. In the mean time have a look into;

-adding + retrieving cookies
-changing the iframes src using something like document.getElementByID('FrameID').src

Goodluck,

Chris
 
Sorry, thinking about it a little more, I have 1 question...

Do you mean that you need adverts to rotate across all users or for each individual user? By that I mean does the advert change every time ANY visitor visits (in which you would probably need to use a server side language such as PHP), or each visitor has their own personal rotation (their first visit they see advert1, second visit advert2 etc etc). Also have you looked into any scripts which have already been written to perform this task. Maybe search advert rotation in Google?

Chris
 
Well I didn't really want to do the cookie thing. this is what im trying to accomplish....

MY friend and I both have splash pages with tons of offers on them.
lets say they are splash1 and splash2.

We are sharing the same url and sharing the work load to get traffic for it. So in order for it to be fair. We would like it to change from splash1 to splash2 each time the page is refreshed or open.

I was planning on making the iframe as tall and wide as the page with no scroll bars, so people wouldnt know its like an iframed webpage....

but ya so what im trying to do is just everytime the page gets a hit, whether its from a refresh or newly opened, it changes from splash1 to splash2.

The first code I entered and posted worked perfect, just when you only have 2 diff iframes, it seems that 1 would always come up and not the other (since it was like choosing randomly).


Thank you so much for your help on everything btw, I really do appreciate it. I usually can find out about stuff on my own, but totally stuck on this for some reason. So thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top