1

After my dashboard initializes and is fully loaded, I need to get the window height of the embed inside the iframe. Ideally, I'd like to get innerHeight inside of onFirstInteractive, but am unable to do so.

function initViz() {
    var containerDiv = document.getElementById("vizContainer");
    var url = "http://public.tableau.com/views/RegionalSampleWorkbook/Storms";

    var options = {
        onFirstInteractive: function() {           
            // How do I get the height of the rendered contents?
        }
    };

    var viz = new tableau.Viz(containerDiv, url, options);
}
Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61
  • Did you try `this.contentWindow.document.body.scrollWidth` and `this.contentWindow.document.body.scrollHeight`? If the code is called inside the iFrame, then that's how you get the window size. – Christos Lytras Jun 19 '20 at 17:26
  • @ChristosLytras `this` is unfortunately undefined. – Andy Hoffman Jun 19 '20 at 18:31
  • This is a different question but check te answers, it might help...obj.contentWindow.document.documentElement.scrollHeight. https://stackoverflow.com/questions/9975810/make-iframe-automatically-adjust-height-according-to-the-contents-without-using – Alucard777 Jun 20 '20 at 15:00

1 Answers1

2

Subscribe to the VIZ_RESIZE event, it provides the new dimensions of the iframe on initialization and resize:

viz.addEventListener(tableau.TableauEventName.VIZ_RESIZE, function(event) {
    console.log(event.getAvailableSize());
});

Which gives the following:

console

When the iframe appears like this:

iframe

If you absolutely want to retrieve the information in onFirstInteractive, you can do this:

onFirstInteractive: function(viz) {           
    const iframeStyle = viz.$1._impl.$1h.style;
    const { height, width } = iframeStyle;
    console.log({ height, width });
}

But it's a little bit hacky because this solution uses properties that are not supposed to be public, so this kind of code may break on future Tableau JS library updates.

Abdul Moeez
  • 1,331
  • 2
  • 13
  • 31
Guerric P
  • 30,447
  • 6
  • 48
  • 86