Link Search Menu Expand Document

PDF

Synchronous JavaScript Ad Tags

Publishers


Synchronous JavaScript ad tags run in series with the execution of the overall loading of a page when delivering an ad for an ad unit. They provide the following benefits:

  • Compatible with third-party tags that use document.write().
  • Allow render-time access to the browser document object model (DOM).
  • They are processed in a predictable sequence.

Standalone Synchronous JavaScript Ad Tag

You can use the OpenX UI to generate tags, which is recommended if you want to use it with third-party ad servers or any time an OpenX JavaScript ad tag is wrapped in a document.write() call.

Click to expand example
<script type="text/javascript">
if (!window.OX_ads) OX_ads = [];
OX_ads.push({"auid":"4" });
</script>
<script src="http://delivery_server_domain/w/1.0/jstag"></script>

Where:

  • OX_ads: A global array to store ad request details.

  • OX_ads.push: Adds a new ad unit to the ad request queue.

  • ({"auid":"4"}): Ad unit ID of the ad to be retrieved.

  • src="http://delivery_server_domain/w/1.0/jstag: A request to fetch the OpenX JavaScript tag library.

The OpenX JavaScript tag library scans the OX_ads array and executes ad requests via the public requestAd method.

NOTE

To avoid namespace collisions, you can add the oxns parameter to this request. However, you do not need to manually change the references to the object on the page.


Standalone Synchronous JavaScript Tag with Multiple Parameters

The parameters in the following tag are supported by the requestAd method.

<script type="text/javascript">
if (!window.OX_ads) OX_ads = [];
OX_ads.push({
"auid":"12345",
"tid":"4",
"tg":"_blank",
"r":"http://redirect_clicks_to_here/landing.html",
"rd":"120",
"rm":"2",
"imp_beacon":"<iframe src='myplace.com'></iframe>",
"fallback":"<iframe src=/test/ads/468x60-unicef.gif></iframe>",
"vars":{"key1":"value1","key2":"value2"}
});
</script>
<script src="http://delivery_server_domain/w/1.0/jstag"></script>

Standalone ad tag using OX static methods

Click to expand example
<script type="text/javascript"
src="http://delivery_server_domain/w/1.0/jstag"></script>

<script type="text/javascript">
OX.requestAd({"auid":"8"});
</script>

<noscript>
<iframe id="4cb4e94bd5bb6" name="4cb4e94bd5bb6"
src="http://delivery_server_domain/w/1.0/afr?auid=8&target=_blank&cb=INSERT_RANDOM_NUMBER_HERE"
frameborder="0" scrolling="no" width="728" height="90">
<a href="http://delivery_server_domain/w/1.0/rc?cs=4cb4e94bd5bb6&cb=INSERT_RANDOM_NUMBER_HERE"
target="_blank">
<img src="http://delivery_server_domain/w/1.0/ai?

auid=8&cs=4cb4e94bd5bb6&cb=INSERT_RANDOM_NUMBER_HERE"
border="0" alt="">
</a>
</iframe>
</noscript>

Where:

  • src="http://delivery_server_domain/w/1.0/jstag": The location of the OpenX JavaScript tag library.

  • OX.requestAd: The method that invokes an ad request.

  • ({"auid":"8"}): Ad unit ID to retrieve a single ad.

  • <noscript/>: Includes fallback iframe tags to use if the browser does not support JavaScript.


Standalone Ad Tag Using OX Instance Methods

Click to expand example
<script type="text/javascript"
src="http://delivery_server_domain/w/1.0/jstag">
</script>

<script type="text/javascript">
var OX_aa07b157 = OX();
OX_aa07b157.addAdUnit("12345");
OX_aa07b157.fetchAds();
</script>

<script type="text/javascript">
OX_aa07b157.showAdUnit("12345");
</script>

Where:

  • var OX_aa07b157 = OX();: Creates a new OX object and assigns it to a local variable, using a pseudo-random name in this example.

  • OX_aa07b157.addAdUnit("12345");: Queues a request for ad unit ID 12345.

  • OX_aa07b157.fetchAds();: Executes the call to the Ad Server for the queued ad units.

  • OX_aa07b157.showAdUnit("12345");: Displays the selected ad for the ad unit with ID 12345.


Multi-ad Unit Synchronous JavaScript Tags

In cases where coordination is required between ad units (for example, competitive exclusion) or if you want to reduce the number of calls made per page, you can use multi-ad unit tags.

To implement a multi-ad unit request in your website source code:

  • Paste the header tag in the header before the anchor tags.
  • Paste the anchor tags between the HTML body tags where you want to ads to be displayed. Failure to implement the appropriate anchor tags may result in a discrepancy between the number of requests and impressions you see in your delivery reports.
Click to expand example
<html>
<head>

<!-- Load the OpenX JavaScript tag library -->

<script src="http://delivery_server_domain/w/1.0/jstag"></script>

<script>
/* Create the OX object. The variable name is randomly
generated to prevent global name collisions */

var OX_aa07b157 = OX();

/* Add some ad units */
OX_aa07b157.addAdUnit("12345");
OX_aa07b157.addAdUnit("54321");

/* Request an array from the Ad Server and create DOM nodes.
The ads returned are stored for later display. */

OX_aa07b157.fetchAds();
</script>

</head>

<body>
Some content here

/* Show the ad for the specified ad unit (an inline write of the ad+beacon). If JavaScript does not run in the browser, fallback to an iframe. If that fails, fallback to an img. */

<script>
OX_aa07b157.showAdUnit("12345");
</script>

<noscript>
<iframe id='a62bc7d3' name='a62bc7d3'
src='http://delivery_server_domain/w/1.0/afr?auid=12345&cb=INSERT_RANDOM_NUMBER_HERE'
frameborder='0' scrolling='no' width='728' height='90'>
<a href='http://delivery_server_domain/w/1.0/rc?cs=acd22faf&cb=INSERT_RANDOM_NUMBER_HERE'
target='_blank'>
<img src='http://delivery_server_domain/w/1.0/ai?

auid=12345&cs=acd22faf&cb=INSERT_RANDOM_NUMBER_HERE'
border='0' alt='' />
</a>
</iframe>
</noscript>

Some content here

<!-- Show the ad for ad unit 54321 -->
<script>
OX_aa07b157.showAdUnit("54321");
</script>
<noscript>
<iframe id='a62ff7d3' name='a62ff7d3'
src='http://delivery_server_domain/w/1.0/afr?auid=54321&cb=INSERT_RANDOM_NUMBER_HERE'
frameborder='0' scrolling='no' width='728' height='90'>
<a href='http://delivery_server_domain/w/1.0/rc?cs=bcd21faf&cb=INSERT_RANDOM_NUMBER_HERE'
target='_blank'>
<img src='http://delivery_server_domain/w/1.0/ai?

auid=54321&cs=bcd21faf&uniq=INSERT_RANDOM_NUMBER_HERE'
border='0' alt='' />
</a>
</iframe>
</noscript>
</body>
</html>

Where:

  • var OX_aa07b157 = OX();: Creates a new OX object and assigns it to a local variable with a pseudo-random name.

  • OX_aa07b157.addAdUnit("12345");: Queues a request for ad unit 12345 in the anchor location, using the previously declared instance of the OpenX JavaScript tag library.

  • OX_aa07b157.addAdUnit("54321");: Queues a request for ad unit 54321.

  • OX_aa07b157.fetchAds();: Executes the call for the queued ad units.

If you want to implement a fallback mechanism, place the appropriate code after each anchor tag in your source code.