-1

It may look like a simple question at first sight but it is not; I got the following code:

window.intercomSettings = {
"app_id": "ewqfsdjh",
"user_id": "user",
"real_name": " ",
"name": "user",
"email": "user@user.com",
"created_at": 225028599901,
};
(function() {
var w = window;
var ic = w.Intercom;
if (typeof ic === "function") {
    ic('reattach_activator');
    ic('update', intercomSettings);
} else {
    var d = document;
    var i = function() {
        i.c(arguments)
    };
    i.q = [];
    i.c = function(args) {
        i.q.push(args)
    };
    w.Intercom = i;

    function l() {
        var s = d.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = 'https://widget.intercom.io/widget/ewqfsdjh';
        var x = d.getElementsByTagName('script')[0];
        x.parentNode.insertBefore(s, x);
    }
    if (w.attachEvent) {
        w.attachEvent('onload', l);
    } else {
        w.addEventListener('load', l, false);
    }
}
})()

http://js.do/diomerda/81708 which outputs quite a lot of html.

Say I'd like to delete (or hide) the entire button (intercom-container div) by javascript once the page gets loaded. How would you accomplish this?

I tried

window.onload = function(){document.getElementById('intercom-container').outerHTML='';}

and many variations, but the button keeps respawning...

Maybe I could append some code to modify the button respawn behaviour.

porcodio
  • 3
  • 2

2 Answers2

1

The snippet you posted does not by itself generate any HTML. It asynchronously loads a library responsible for bootstrapping the interface. window.onload fires before the library is loaded, so you are effectively trying to hide the element before it's ever created.

Instead, looking at the API docs, you can use

Intercom('shutdown');

This is both safer and cleaner than trying to hide the element yourself.

Igor Raush
  • 15,080
  • 1
  • 34
  • 55
  • Ok, thank you very much indeed, this clarifies a lot. So, which code should I place to be sure that Intercom('shutdown'); is executed after the library is loaded? – porcodio Feb 02 '16 at 01:19
  • Since unfortunately I just can not write that instruction right after the rest of the code, but just before it. – porcodio Feb 02 '16 at 01:27
  • `Intercom('shutdown')` seems to be deferred until the library is loaded, so there is no need to bind to any events or wait until the library is loaded. Why can't you place this line right after the Intercom loader? – Igor Raush Feb 02 '16 at 01:29
  • Because the intercom script is automatically added at the bottom of the page (via a server side script which I can not manage). Anyway you pointed out the exact way to go! Thank you very much indeed. I'll probabily get an answer here http://stackoverflow.com/questions/7486309/how-to-make-script-execution-wait-until-jquery-is-loaded for the last part of my issue. – porcodio Feb 02 '16 at 01:44
  • just for reference I ended up using the following code: function downloadJSAtOnload() { var element = document.createElement("script"); element.innerHTML = "Intercom('shutdown');"; document.body.appendChild(element); } if (window.addEventListener) window.addEventListener("load", downloadJSAtOnload, false); else if (window.attachEvent) window.attachEvent("onload", downloadJSAtOnload); else window.onload = downloadJSAtOnload; – porcodio Feb 02 '16 at 02:11
  • That seems like serious overkill... If you want to just defer some code until the next event loop, use `setTimeout(function () { Intercom('shutdown'); })` ([demo](http://js.do/code/81713)). This is not more reliable than your approach, but it's definitely cleaner. The combination of waiting for the `window.onload` event, and then injecting more code into the parser using an extra ` – Igor Raush Feb 02 '16 at 02:31
0

window.onload = function(){ document.getElementById('intercom-container').style.display = 'none';