Getting width of page in iframe

Hi,

I am trying to get the width of the page in an iframe (on the same domain) using the following:

document.getElementById('iframe').clientWidth

but it seems not to be working. The iframe width is a constant but I want to get the page width based on whether there is a scrollbar or not. If there is not scrollbar, the code should give the fixed iframe width, if there is a scrollbar (which means the page content height is greater than iframe height), the code should give fixed iframe width minus 17 (scrollbar width).

Any ideas how to do that?

Thanks.

Hi,

Are you using jQuery?

if so, you can just do this:

console.log(window.parent.$("#iframeID").width());

HTH

Doesn’t that give the width of the iframe itself? I need the width of the page within the iframe. For example, the following code returns the window width and window width minus the scrollbar, I am trying to do the same for the iframe if possible.

alert(window.innerWidth+', '+document.body.clientWidth);

By the way, how can I get that console log value and use it? Tried assigning it to a variable but didn’t work.

Oh, sorry!
Try this:

$(document).ready(function() { 
  $('iframe').load(function() {
    console.log($("iframe").contents().width());	
  });
});

It should do.
You could do this:

$(document).ready(function() { 
  $('iframe').load(function() {
    var w = $("iframe").contents().width();
    console.log(w);
  });
});