0

I had the code below placed in the index.html and it works.

<!-- Smartsupp Live Chat script -->
<script type="text/javascript">
    var _smartsupp = _smartsupp || {};
    _smartsupp.key = '';
    window.smartsupp ||
        (function (d) {
            var s,
                c,
                o = (smartsupp = function () {
                    o._.push(arguments);
                });
            o._ = [];
            s = d.getElementsByTagName('script')[0];
            c = d.createElement('script');
            c.type = 'text/javascript';
            c.charset = 'utf-8';
            c.async = true;
            c.src = 'https://www.smartsuppchat.com/loader.js?';
            s.parentNode.insertBefore(c, s);
        })(document);
</script>

How can I move that code to be executed from inside a component?

I tried put the code inside a function for example ngAfterViewInit() but I get errors like Property 'smartsupp' does not exist on type 'Window & typeof globalThis'.

export class AppComponent  {
  

  ngAfterViewinit(){

    var _smartsupp = _smartsupp || {};
        _smartsupp.key = '';
        window.smartsupp ||
            (function (d) {
                var s,
                    c,
                    o = (smartsupp = function () {
                        o._.push(arguments);
                    });
                o._ = [];
                s = d.getElementsByTagName('script')[0];
                c = d.createElement('script');
                c.type = 'text/javascript';
                c.charset = 'utf-8';
                c.async = true;
                c.src = 'https://www.smartsuppchat.com/loader.js?';
                s.parentNode.insertBefore(c, s);
            })(document);
 
  }

}

1 Answer 1

1

You can move your code to a js file startup.js

and then include the script on your TS file in ngInit

var script = document.createElement('script');
script.src = "assets/startup.js";
document.head.appendChild(script);

and in your script you have smartsupp object added in window

so you can make a condition on it to make sure it s loaded

    if(window && window.smartsupp)
    {
      // do something 
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.