src/ads/insertAds.js
function insertAds(adUnit, ads, adLoadedCallback) {
    var adContainers = page.getAdContainers(adUnit);

    if (!adContainers.length) {
        logger.error("No ad containers could be found. stopping insertion for adUnit " + adUnit.name);
        return []; //Ad can't be inserted
    }
    
    var adBuilder = new AdBuilder(ads);

    var beforeElement;
    var insertedAdElements = [];
    for (var i = 0; i < adContainers.length; i++) {
        var adContainer = adContainers[i];
        while ((beforeElement = adContainer.getNextElement()) !== null) {
            var adToInsert = adBuilder.createAdUnit(adUnit, adContainer.containerElement);

            if (adToInsert === null) {
                //we ran out of ads.
                break;
            }

            insertedAdElements.push(adToInsert);
            beforeElement.parentNode.insertBefore(adToInsert, beforeElement.nextSibling);

            // var elementDisplay = adToInsert.style.display || "block";
            //TODO: Why are we defaulting to block here?
            // adToInsert.style.display = elementDisplay;
            handleImageLoad(adToInsert, adLoadedCallback);
        }
    }

    return insertedAdElements;
}
function insertAds(adUnit, ads, adLoadedCallback) {
    var adContainers = page.getAdContainers(adUnit);

    if (!adContainers.length) {
        logger.error("No ad containers could be found. stopping insertion for adUnit " + adUnit.name);
        return []; //Ad can't be inserted
    }
    
    var adBuilder = new AdBuilder(ads);

    var beforeElement;
    var insertedAdElements = [];
    for (var i = 0; i < adContainers.length; i++) {
        var adContainer = adContainers[i];
        while ((beforeElement = adContainer.getNextElement()) !== null) {
            var adToInsert = adBuilder.createAdUnit(adUnit, adContainer.containerElement);

            if (adToInsert === null) {
                //we ran out of ads.
                break;
            }

            insertedAdElements.push(adToInsert);
            beforeElement.parentNode.insertBefore(adToInsert, beforeElement.nextSibling);

            // var elementDisplay = adToInsert.style.display || "block";
            //TODO: Why are we defaulting to block here?
            // adToInsert.style.display = elementDisplay;
            handleImageLoad(adToInsert, adLoadedCallback);
        }
    }

    return insertedAdElements;
}
function insertAds ( adUnit , ads , adLoadedCallback ) { var adContainers = page . getAdContainers ( adUnit ) ; if ( ! adContainers . length ) { logger . error ( "No ad containers could be found. stopping insertion for adUnit " + adUnit . name ) ; return [ ] ; //Ad can't be inserted } var adBuilder = new AdBuilder ( ads ) ; var beforeElement ; var insertedAdElements = [ ] ; for ( var i = 0 ; i < adContainers . length ; i ++ ) { var adContainer = adContainers [ i ] ; while ( ( beforeElement = adContainer . getNextElement ( ) ) !== null ) { var adToInsert = adBuilder . createAdUnit ( adUnit , adContainer . containerElement ) ; if ( adToInsert === null ) { //we ran out of ads. break ; } insertedAdElements . push ( adToInsert ) ; beforeElement . parentNode . insertBefore ( adToInsert , beforeElement . nextSibling ) ; // var elementDisplay = adToInsert.style.display || "block"; //TODO: Why are we defaulting to block here? // adToInsert.style.display = elementDisplay; handleImageLoad ( adToInsert , adLoadedCallback ) ; } } return insertedAdElements ; }
Insert advertisements for the given ad unit on the page
@param adUnit - The ad unit to insert advertisements for
@param ads - Array of ads retrieved from OfferEngine
@param adLoadedCallback - Callback to execute when the ads are fully loaded
@returns {Array}
Insert advertisements for the given ad unit on the page