KevInSol
KevInSol

Reputation: 2644

Setting innerHTML to Javascript code and then runnng it / dynamically load an ad call

I have a site with banner ads on it. What I need to do is rotate those ads - ie you stay on the homepage (no reload) and every 10 seconds a new ad (from a set of 3 or 4) is displayed.

What I do is have a Javascript function that puts my ad code in an array and cycles through the arrary every 10 seconds and does this (just showing the one line of code relevent to my question):

document.getElementById(DivID).innerHTML = AdText;

DivID is the name of a in the HTML and AdText is the HTML code to display the ad.

This works fine for ads that are images where the ad call is just basically an <img> tag, or Flash/SWFobject embed

But, some ads are JS code supplied by a third party ad agency, for example:

<script language='javascript1.1' src="http://example.net/adj/b4207992.2;sz=468x60;ord=[timestamp]?"></script>

So, basically I'm saying document.getElementById(DivID).innerHTML = "THE ABOVE JS CODE"

Nothing is displayed and I have FF with Firebug and no errors are being displayed.

My guess is that document.getElementById(DivID).innerHTML is setting that code alright (as I have additional code to call a 1x1 image to track impressions and this is logging an impression), but that it is just not executing, maybe JS needs some event like a page load to start it?

Any ideas or alternatives?

Upvotes: 1

Views: 1080

Answers (1)

Konstantin Schubert
Konstantin Schubert

Reputation: 3356

The reason is that the new content is not being interpreted. If you use jQuery, .html() should solve your problem. This link explains it much better: Difference between innerHTML and .html() from jQuery

Upvotes: 1

Related Questions