javascriptangularangular-material2amazon-advertising-api

Placing Amazon Banner | Angular V4


I'm placing an Amazon banner inside an angular material 2 card.But the problem is that it is not rendering.It shows empty div.What could be the reason.Below is the code showing how I did it.

<md-card class="full-width full-height border-box ">
    <div class="adv">
        <script type="text/javascript" language="javascript">
            var aax_size = '728x90';
            var aax_pubname = 'XXXXXXXXXXX';
            var aax_src = '302';
          </script>
          <script type="text/javascript" language="javascript" src="http://c.amazon-adsystem.com/aax2/assoc.js"></script>
    </div>
  </md-card>

I also tried to to bind it using property binding

<span [innerHTML]="advertisement()"></span>
advertisement(){
  return `<div class="adv">
        <script type="text/javascript" language="javascript">
            var aax_size = '728x90';
            var aax_pubname = 'XXXXXXXXXXX';
            var aax_src = '302';
          </script>
          <script type="text/javascript" language="javascript" src="http://c.amazon-adsystem.com/aax2/assoc.js"></script>
    </div>`;

}

I also tried to dynamically add the div inside my card,it shows inside the div but the banner doesn't appear.Below is the code showing how I did that.

ngAfterViewInit() {
    let x: HTMLElement = document.getElementById('adv');
    let s: HTMLScriptElement = document.createElement('script');
    s.type = 'text/javascript';
   // s.language = 'javascript';
    let code = `var aax_size = '728x90';
                var aax_pubname = 'XXXXXXX';
                var aax_src = '302';`;
    let src = document.createElement('script');
    src.type = 'text/javascript';
   // src.language = 'javascript';
    src.src = 'http://c.amazon-adsystem.com/aax2/assoc.js';             
    try {
        s.appendChild(document.createTextNode(code));
        x.appendChild(s);
        x.appendChild(src);
    } catch (e) {
        s.text = code;
        document.body.appendChild(s);
    }
    console.log(x);
}

Solution

  • After scrapping every post in SO regarding or similar to this question I did not find any solution to this.I followed almost everything in those posts but nothing was working for me.After that I came across postscribe library which does the externally loading of any third party script.

    First I installed the library and imported it in my component

    import * as postscribe from 'postscribe';
    

    After that all I did was calling a function inside my ngAfterViewInit function, by targetting the div with its id which in my case was adv and passed the script as a second parameter to this function.

    ngAfterViewInit() {
    postscribe('#adv', `<script type="text/javascript" language="javascript">
                           var aax_size='728x90';
                           var aax_pubname = 'XXXXXXXX';
                           var aax_src='302';
                        </script>
                        <script type="text/javascript" language="javascript" src="http://c.amazon-adsystem.com/aax2/assoc.js"></script>`);}
    

    By doing this my banner was loaded.