/    Sign up×
Community /Pin to ProfileBookmark

Random iframe adverts

Hi

I want to display two advert networks on my page, Google AdSense and also my own network. I am using this script to display at random different pages with the ad networks own scripts:

[QUOTE]

<script language=”JavaScript1.2″>

var ie=document.all&&navigator.userAgent.indexOf(“Opera”)==-1
var dom=document.getElementById&&navigator.userAgent.indexOf(“Opera”)==-1

var iframeprops=’width=151 height=125 marginwidth=”0″ marginheight=”0″ hspace=”0″ vspace=”0″ frameborder=”0″ scrolling=”no”‘

var randomcontent=new Array()
randomcontent[0]=”ads/home/google.html”
randomcontent[1]=”ads/home/evolution.html”

if (ie||dom)
document.write(‘<iframe id=”dynstuff” src=”” ‘+iframeprops+’></iframe>’)

function random_iframe(){
if (ie||dom){
var iframeobj=document.getElementById? document.getElementById(“dynstuff”) : document.all.dynstuff
iframeobj.src=randomcontent[Math.floor(Math.random()*randomcontent.length)]
}
}

window.onload=random_iframe

</script>

[/QUOTE]

The problem I have is the Google adverts are 125×125 and my networks adverts are 151×70 pixels.

Is there any way to change the height and width of the iframe depending upon which advert page is selected at random? ?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Warren86Nov 14.2004 — function changeDimensions(nW,nH){

window.frames['dynstuff'].width = nW;

window.frames['dynstuff'].height = nH;

}

if (iframeobj.src == "ads/home/Google.html")

{changeDimensions(125,125)}

else {changeDimensions(150,170)}
Copy linkTweet thisAlerts:
@Warren86Nov 14.2004 — My error, it should be like this:

function changeDimensions(nW,nH){

window.frames['dynstuff'].resizeTo(nW,nH);

}
Copy linkTweet thisAlerts:
@pwnetauthorNov 14.2004 — Thanks for quick reply!

I added your last amendment to the script, the script is now:

<script language="JavaScript1.2">

var ie=document.all&&navigator.userAgent.indexOf("Opera")==-1

var dom=document.getElementById&&navigator.userAgent.indexOf("Opera")==-1

var iframeprops='width=151 height=70 marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no"'

var randomcontent=new Array()

randomcontent[0]="ads/home/google.html"

randomcontent[1]="ads/home/evolution.html"

function changeDimensions(nW,nH){

window.frames['dynstuff'].resizeTo(nW,nH);

}

if (changeDimensions.src == "ads/home/Google.html")

{changeDimensions(125,125)}

else {changeDimensions(151,70)}

if (ie||dom)

document.write('<iframe id="dynstuff" src="" '+iframeprops+'></iframe>')

function random_iframe(){

if (ie||dom){

var iframeobj=document.getElementById? document.getElementById("dynstuff") : document.all.dynstuff

iframeobj.src=randomcontent[Math.floor(Math.random()*randomcontent.length)]

}

}

window.onload=random_iframe

</script>[/QUOTE]
When I run the page (test page – http://www.evolution-online.co.uk/test/index.html - ads should appear under the title “Advertisements”) noting shows, the error is:

Line: 211

Char: 1

Error: ‘window.frames.dynstuff’ is null or not an object

Code: 0

Now there’s a test page you can see the code and what I want to do.

So what does this mean?
Copy linkTweet thisAlerts:
@Warren86Nov 14.2004 — Try changing this line:

document.write('<iframe id="dynstuff" src="" '+iframeprops+'></iframe>')

To this:

document.write('<iframe name="dynstuff" src="" '+iframeprops+'></iframe>')

The Iframe should have a NAME not an ID.

And this line is wrong:

if (changeDimensions.src == "ads/home/Google.html")

changeDimensions is the name of the function, it has no SRC.

The SRC you should be testing for, was the way I first posted it: The Iframe SRC.

And, I don't get involved with anything "Netscape". All I'm suggesting is that you identify the SRC for the IFRAME, you seem to be able to change that to one of the two array elements, then just call the changeDimensions function, passing the appropriate two values, for height and width, based upon what the Iframe SRC is.

I have nothing further to offer.
Copy linkTweet thisAlerts:
@Warren86Nov 14.2004 — This is the way I would do it. If it doesn't work in NS, can't help you.

<HTML>

<Head>

<Script Language=JavaScript>

var iframeProps="<IFrame name='adFrame' Src='' width=151 height=125 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=1 scrolling='no'></IFrame>";

var randomcontent=new Array()
randomcontent[0]="test1.html"
randomcontent[1]="test2.html"

function randomAd(){

isAdSrc =0+Math.round(Math.random()*1)
window.frames['adFrame'].location = randomcontent[isAdSrc];
if (isAdSrc == 0){changeDimensions(151,125)}
if (isAdSrc == 1){changeDimensions(120,145)}
}

function changeDimensions(nW,nH){

window.frames['adFrame'].resizeTo(nW,nH);
}

document.write(iframeProps);

window.onload=randomAd;


</Script>

</Head>

<Body>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@pwnetauthorNov 15.2004 — Thanks for that! It works on my computer, but when I upload it - http://www.evolution-online.co.uk/ - it only displays the Google ads, the iframe for the other ads page doesn’t show. ?
Copy linkTweet thisAlerts:
@Warren86Nov 15.2004 — I went to your site. The first two times I refreshed, I saw ads sponsored by Google. On the third refresh, I saw your crossfade slideshow. Seems to be working.
Copy linkTweet thisAlerts:
@pwnetauthorNov 15.2004 — I went to the site again, and yes they all work! ?

Thanks for all your help Warren86, much appreciated.
×

Success!

Help @pwnet spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...