Auto-resize HTML frame depending on window size

xenium
xenium used Ask the Experts™
on
hi,

I'd like the resize this frame depending on the window size, how can i do this?

URL: http://enex.net/autorecon/fusion/bm_network.html

<iframe width="1000" height="600" scrolling="no" frameborder="no" src="https://fusiontables.google.com/embedviz?containerId=googft-gviz-canvas&amp;viz=GVIZ&amp;t=GRAPH&amp;gc=true&amp;gd=true&amp;sdb=1&amp;rmax=100000&amp;q=select+col0%2C+col2+from+1pNOeGhfGxNU9RvFgGMC5egdSprZL-6q50jt9anr5&amp;qrs=+where+col0+%3E%3D+&amp;qre=+and+col0+%3C%3D+&amp;qe=&amp;uiversion=2&amp;state=%7B%22ps%22%3A%221_0_1w_1_t_3a_1_x_-f_u_5_k_d_w_-c_1_m_13_-u_12_-16_b_16_e_-1i_8_2j_s_9_2o_m_4_1n_y_3_1x_-10_2_10_-c_a_2p_-j_6_20_z_q_1d_-q_b_2l_-p_c_1a_s_d_2a_-x_e_2s_-c_f_1t_11_g_27_z_y_3z_-k_i_1q_-x_j_23_-x_k_2t_-3_l_2d_v_n_2f_-s_o_15_-j_p_11_e_r_1j_-w_s_2r_g_u_1g_w_v_y_-4_13_-15_1f_z_46_9_10_3z_n_11_46_-6_1_z_5_14_-p_1p_15_-1b_z_7_2t_8_17_-e_-1x_h_15_m_%22%2C%22cx%22%3A85.96027096386045%2C%22cy%22%3A8.76855179300828%2C%22sw%22%3A639.789842687397%2C%22sh%22%3A245.5175468552088%2C%22z%22%3A0.999142241431289%7D&amp;gco_forceIFrame=true&amp;gco_hasLabelsColumn=true&amp;width=1000&amp;height=600"></iframe>

Open in new window


Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Remove the local width and height attributes from the tag and use CSS to specify them.  Then you can use media queries to specify alternate CSS based on the width of the device used for viewing. you could also do the change during load with javascript to test viewport width and either change the specific attributes or swap classes.  If you use javascript you will need to add an id to the iframe tag so you can get a handle on it.

Author

Commented:
Great thanks, I'm new to javascript, could you link to an example page that does, and mention the main elements, so I can try to copy the code over?
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
You can make the iframe the size of the window by doing this
body, html {
  height: 100%;
  min-height: 100%;
}
iframe {
  height: 100%;
  width: 100%;
  border: 0;
}

Open in new window

However that is only going to affect the size of the container. The height of the content is specified by the parameters passed to it in the URL so to fix that you are going to need to send it the right width and height which means we need to use JavaScript
<script>
document.addEventListener('load', function() {
  var iframe = document.getElementsByTagName('iframe');
  var width = window.innerWidth;
  var height = window.innerHeight;
  var src = iframe[0].src;
  var newsrc = src.replace(/width=(\d+)&height=(\d+)/,'width=' + width+ '&height=' + height);
  iframe[0].src = newsrc;
}
</script>

Open in new window


Here is a full listing
<html>
<head>
<style type="text/css">
* {
	margin: 0;
	padding: 0;
}
body, html {
  height: 100%;
  min-height: 100%;
}
iframe {
  height: 100%;
  width: 100%;
  border: 0;
}
</style>
</head>
<body>
<iframe scrolling="no" frameborder="no" src="https://fusiontables.google.com/embedviz?containerId=googft-gviz-canvas&amp;viz=GVIZ&amp;t=GRAPH&amp;gc=true&amp;gd=true&amp;sdb=1&amp;rmax=100000&amp;q=select+col0%2C+col2+from+1pNOeGhfGxNU9RvFgGMC5egdSprZL-6q50jt9anr5&amp;qrs=+where+col0+%3E%3D+&amp;qre=+and+col0+%3C%3D+&amp;qe=&amp;uiversion=2&amp;state=%7B%22ps%22%3A%221_0_1s_1_x_-k_s_t_37_1_5_g_c_w_-f_-2_m_12_-x_12_-1a_8_16_h_-1o_8_2f_t_9_2j_n_4_1j_y_3_1u_-z_2_w_-c_a_2m_-i_6_1w_y_q_18_-p_b_2i_-o_c_16_s_d_27_-x_e_2p_-b_f_1p_11_g_23_10_h_11_m_i_1n_-x_j_20_-w_k_2q_-1_l_28_v_n_2c_-r_o_10_-j_p_x_f_r_1f_-v_s_2n_h_u_1c_w_v_u_-4_13_-19_1c_z_42_9_10_3v_o_11_43_-6_1_v_5_14_-u_1n_15_-1g_w_7_2p_9_17_-8_-29_y_3v_-k_%22%2C%22cx%22%3A85.96027096386045%2C%22cy%22%3A8.76855179300828%2C%22sw%22%3A639.789842687397%2C%22sh%22%3A245.5175468552088%2C%22z%22%3A0.999142241431289%7D&amp;gco_forceIFrame=true&amp;gco_hasLabelsColumn=true&amp;width=1000&amp;height=600"></iframe>
<script>
window.addEventListener('load', function() {
  var iframe = document.getElementsByTagName('iframe');
  var width = window.innerWidth;
  var height = +window.innerHeight - 0;
  var src = iframe[0].src;
  var newsrc = src.replace(/width=(\d+)&height=(\d+)/,'width=' + width+ '&height=' + height);
  console.log(newsrc);
  iframe[0].src = newsrc;
});
</script>
</body>

Open in new window

However, if you have control over the iframe src you can just leave the width and height off and add it like this - much simpler

<script>
window.addEventListener('load', function() {
  var iframe = document.getElementsByTagName('iframe');
  iframe[0].src += '&width=' + window.innerWidth + '&height=' + window.innerHeight;
});
</script>

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Great thanks a lot, that works :-)
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial