Dynamically Inject Ad Tags


A. Dynamically Inject Ad Tags for more flexibility

If needed, you can dynamically inject the entire ad tag into your site by wrapping it in a JavaScript function and separating it from its original placement in the code.

<html>
<head></head>
<body>
<!-- SOLADS-ADVERT -->
<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  = "https://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>


(#91)

ContactSign upTerms