Load or Launch Ad Tags with Javascript (and target element for tag)

Let’s say, upon a button click, I want to place an ad tag (javascript code) into a div element and have it actually run?

Is there a simplified way to do that?

Cheers!
Ryan

Something like this? You just need to put all of the ad html into the page with innerHTML, the scripts you’ll need to append a new script to have them run.

var button = document.getElementById('my-ad-launching-button');
var addRoot = document.getElementById('my-ad');
button.addEventListener('click', function(event) {
  event.preventDefault();
  addRoot.innerHTML = '<div>Ad</div>';

  var script = document.createElement('script');
  script.src = "http://ad-factory.com/script.js";
  document.body.appendChild(script);
});

Thanks. I’m slightly confused, as I don’t see how we are targeting a particular div area.

Like, I have a div with id “leaderboard”, and I’d like the entire script to run as if placed in there… so

function() {
    var script = document.createElement('script');
    script.src = "http://ad-factory.com/script.js";
    document.getElementById("leaderboard").appendChild(script);
}

Like that? And the script will append and run?

Cheers!
Ryan

If I’m not mistaken, that’s what adRoot is. It’s looking for an empty tag with the ID my-ad. The rest of the script places the ad HTML into that tag by accessing its innerHTML property.

1 Like

You’re not mistaken - that is exactly what that code is doing.

Ok, but then shouldn’t above be…

document.addRoot.appendChild(script);

??
Cheers!
Ryan

No it should be

document.body.appendChild(script);

You should always append scripts to the bottom of the page as that’s where scripts go.

And that’s the issue, ad network javascript tags have to appear where the ad is to appear, they can’t appear somewhere else on page.

These aren’t just function scripts, I’m loading supplied ad scripts from the ad network, and that script creates an ad, like a leaderboard, in whatever div element it is placed inside.

Thanks
Ryan

Sounds like the ad scriupt is prehistoric and still using document.write statements if the script needs to be in the spot where the ad is displayed.

You should place a message on your site telling everyone that only Netscape 4 and earlier browsers are properly supported by the ads.:grinning:

The best option is to ask the ad provider where you can get the 21st century version of their ad script to replace the prehistoric one they have currently provided.

Alternatively you need to look at https://github.com/krux/postscribe in order to be able to convert their antiquated calls into something that will work properly from regular JavaScript at the bottom of the page.

They are all like that, all the networks. We use 9 at the moment, all the same. There is not a one that’s not. Even DFP, who now has the most complicated, they have a bunch of script you can put on bottom of page, but you also need script in the element you plan on having the ad. They give you the tags, and the tags have some auditing that wants to be placed with them.

Now, I run the tags through iframes on the pages, but that has resulted in drops in fill due to new auditing procedures.

Cheers!
Ryan

Please post an example of the ad scripts you are trying to embed.

No they are not - none of the ad networks I use require JavaScript anywhere other than at the bottom of the page. Some say the script needs to go with the HTML tags that it refers to but that isn’t true and the scripts can go anywhere below the HTML including at the bottom of the page.

Why not try the code you have been supplied with placing the HTML where the ad is to go and the associated script at the bottom of the page. Unless they are using antiquated JavaScript their script should still work.

What ad network are you using? Sample tag, said to be placed within the element banner should be:

<script type="text/javascript">
if (!window.OX_ads) { OX_ads = []; }
OX_ads.push({ "auid" : "12345678" });
</script>
<script type="text/javascript">
document.write('<scr'+'ipt src="//sub.adnetwork.net/w/1.0/jstag"><\/scr'+'ipt>');
</script>
<noscript><iframe id="785f485d3b" name="785f485d3b" src="//sub.adnetwork.net/w/1.0/afr?auid=538401046&cb=INSERT_RANDOM_NUMBER_HERE" frameborder="0" scrolling="no" width="728" height="90"><a href="//sub.adnetwork.net/w/1.0/rc?cs=785f485d3b&cb=INSERT_RANDOM_NUMBER_HERE" ><img src="//sub.adnetwork.net/w/1.0/ai?auid=538401046&cs=785f485d3b&cb=INSERT_RANDOM_NUMBER_HERE" border="0" alt=""></a></iframe></noscript>

Here’s another network:

<SCRIPT TYPE="text/javascript">
var cachebuster=new Date().getTime();
document.write('<SCR' + 'IPT SRC="http://ib.adnxs.com/ttj?id=123456&size=728x90&pagetype=ros&cb=' + cachebuster + '" TYPE="text/javascript"></SCR' + 'IPT>');
 </SCRIPT>

Another network that has two scripts. The first goes in head or bottom, the second wheere the ad needs to be.

<script type="text/javascript">
    cdsTag = {  };
    cdsTag.zones = {  };
    cdsTag.zones.as2000003541011 = { "size" : "728x90", "id" : "2000003541011" };
    cdsTag.siteId = 2000100655555;
    cdsTag.keywords = null;
    cdsTag.user = null;
    cdsTag.onLoad = function() {cdsPlaceAd("as2000003541011");};
</script>
//place where ad will show.
<script type="text/javascript" src="http://ad.afy11.net/cdsad.js"></script>

And another tier:

<script type="text/javascript" src="//ap.lijit.com/www/delivery/fpi.js?z=115566&u=domain&width=728&height=90"></script>

These are all big ad networks that deal with thousands of domains. I can add more too. These adjustable?

Cheers!
Ryan

Try using postscribe (which I previously linked to) to dynamically replace the long obsolete document.write statements with some code more suited to more modern browsers like IE5+

You might also point out to the service providers that they appear to have not applied any of the changes to support any of the last five versions of JavaScript and still have their code set up primarily for Netscape 2 browsers.

1 Like

postscribe does look to be a workable option. So, let’s say a page loaded and AJAX refreshes all the content and I want to re-load the ad tag, could I run a postscribe function to swap out the ad and put the script right back in place to be executed again? This is to satisfy AJAX navigation.

And, I’ve gone through all my network ad tags and there was one where it actually comes with a div with a unique ID, that looks to be replaced/modified by an external javascript, so you can put that div element where the ad is to go. So that ad network, Springboard, seems to be using what you consider the better way to go.

Cheers
Ryan

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.