Support > Publisher > Load adverts (ad-tag) dynamically

Load adverts (ad-tag) dynamically (#2110)

(Last Update: 13.07.2017)

A. Inject whole tag dynamically

You can inject the whole advert tag dynamically into your site.



<!-- SOLADS-ADVERT -->
<html>
<head></head>
<body>
<div id="SOLADS-ADVERT-CONTAINER"></div>
<!-- SOLADS-ADVERT-END-->

<!-- SOLADS-SCRIPT -->
<script type="text/javascript">
function load_jsads() {
    var tagElement = document.createElement( 'script' );
    tagElement.type = "text/javascript";
    tagElement.setAttribute("data-params","jsads");

    var varElement = document.createTextNode('m3ads_system = "Solads"; m3ads_partnernumber = 123456; m3ads_sectors = "1234"; m3ads_numberadverts = 1; m3ads_imagewidth = 300; m3ads_imageheight = 250;');
    tagElement.appendChild(varElement);

    var scriptElement = document.createElement( 'script' );
    scriptElement.type = "text/javascript";
    scriptElement.src  = "//cdn.feed.solads.media/resources/scripts/jsAds-1.4.min.js";

    document.getElementById('SOLADS-ADVERT-CONTAINER').appendChild( tagElement );
    document.getElementById('SOLADS-ADVERT-CONTAINER').appendChild( scriptElement );
}
load_jsads();
</script>
<!-- SOLADS-SCRIPT-END -->
</body>
</html>


B. Split parameter block und script call

You can alternatively split the script call from the parameter block and execute it only when a certain event has happened. In the example below ONLY the parameter block is part of your HTML-Code. An eventListener checks if a defined element (YOUR_ELEMENT_ID_HERE) has finished loading. Only after that the call for the jsAds library is appended to the advert container (ADVERT_CONTAINER_ID_HERE) and executed.


<!-- SOLADS-ADVERT -->
<html>
<head></head>
<body>
<div id="SOLADS-ADVERT-CONTAINER">
  <script type="text/javascript" data-params="jsads">
  m3ads_system = "Solads";
  m3ads_partnernumber = 123456;
  m3ads_sectors = "1234";
  m3ads_numberadverts = 1;
  m3ads_imagewidth = 300;
  m3ads_imageheight = 250;
  </script>
</div>
<!-- SOLADS-ADVERT-END -->

<!-- SOLADS-SCRIPT -->
<script type="text/javascript">
function load_jsads() {
var scriptElement = document.createElement( 'script' );
scriptElement.type = "text/javascript";
scriptElement.src  = "//cdn.feed.solads.media/resources/scripts/jsAds-1.4.min.js";
document.getElementById('SOLADS-ADVERT-CONTAINER').appendChild( scriptElement );
}
document.getElementById("YOUR_ELEMENT_ID_HERE").addEventListener("load",load_jsads);
</script>
<!-- SOLADS-SCRIPT-END -->
</body>
</html>