You are here: Developers > Ad Request API > Javascript ad tags > Synchronous JavaScript ad tags

Synchronous JavaScript ad tags

Last updated on June 12, 2015

Synchronous JavaScript ad tags run in series with the execution of the overall loading of a page when delivering an ad for an ad unitThe smallest inventory component that represents the space on a site where ads display.. 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 tagA small piece of code that defines the ad space where ads display on a website. It includes parameters that describe the inventory advertising campaigns can target, which may in turn display ads in the ad space. is wrapped in a document.write() call.

<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>

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

Tip: 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

<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>

Standalone ad tag using OX instance methods

<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>

Multi-ad unit synchronous JavaScript tags

In cases where coordination is required between ad units (e.g., 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.

<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>

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

Ad unit group ad tags

Ad unit groupA collection of ad units where related ads display together at the same time. tags are designed to facilitate ad coordination and reduce page load times. You must use this type of tag to enable companion ad selection.

Use the OpenX UI to generate one header tag and an anchor tags for each ad unit referenced by the ad unit group; then customize them as needed.

Paste the header tag before the anchor tags in the header portion of your website source code and place each anchor tag between the body tags in the locations where you want to display ads.

<html>
<head>

<!-- load the static javascript library, which is cached 
for awhile by the browser -->

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

<script>
/* create an instance of the OX object */
var OX_07e44891 = OX();

/* Queue a request for all ad units referenced by the ad unit group. */

OX_07e44891.addPage("123456");
OX_07e44891.addPage("654321");


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

OX_07e44891.fetchAds();
</script>

</head>

<body>

Some content here

<!-- Show the ad for ad unit 12345 -->
<script>
OX_07e44891.showAdUnit("12345");
</script>

Some content here

<!-- Show the ad for ad unit 54321 -->
<script>
OX_07e44891.showAdUnit("54321");
</script>

</body>
</html>

Feedback form