1

In a cross domain scenario I need to know what's in the iframe size (width and height) when the javascript code running inside the iframe. (I can't climb to the parent because I'm in a cross domain iframe) Note: I don't want to use jquery. I tried several ways to get that info:

var w = window.innerWidth || window.document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth || document.body.offsetWidth
var h = window.innerHeight || window.document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight || document.body.offsetHeight
Alon Ashkenazi
  • 1,223
  • 4
  • 21
  • 29
  • What is `this`? Why don't you provide more code as a context for where this code is and what you're doing? – Ian Dec 10 '12 at 09:58
  • There shouldn't be this. I removed it – Alon Ashkenazi Dec 10 '12 at 10:08
  • So you have Javascript in an iframe. You're trying to get the size of what? The iframe? Or the parent window holding the iframe? – Ian Dec 10 '12 at 10:11
  • Exactly. I'm trying to get the iframe size. – Alon Ashkenazi Dec 10 '12 at 10:12
  • Okay, and is the code you provided returning any values? Or is this just not working in IE? – Ian Dec 10 '12 at 10:13
  • It's just not working on IE. It's work for FF and Chrome – Alon Ashkenazi Dec 10 '12 at 10:14
  • Okay, do you get any errors or just `0` or `""`? Did you know you have some weird code in there? In the middle of the second line, you have `|| viewportheight = document.getElementsByTagName('body')[0].clientHeight` (notice the `viewportheight = ` part), and at the end of the same line, you have `offsetHeight`, not `document.body.offsetHeight` like you do in the first line for `offsetWidth`? – Ian Dec 10 '12 at 10:16
  • I get 0. The weird code is just copy pass mistake. I fixed it in the question body – Alon Ashkenazi Dec 10 '12 at 10:20

2 Answers2

0

So, are you trying to access an iframe on another domain? You can't, because of XSS protection.

Here's a similar question: Get DOM content of cross-domain iframe

http://en.wikipedia.org/wiki/Cross-site_scripting

EDIT: Maybe this solution could give you an hint on what to do http://paulasmuth.com/blog/dynamic_height_crossdomain_iframe/ depending on the permissions you have on your pages.

Community
  • 1
  • 1
RedKnight91
  • 71
  • 1
  • 12
0

If you want to get the window size, use this(excluding the scrollbar size if there is):

var w = document.documentElement.clientWidth;
var h = document.documentElement.clientHeight;

If you want to get the document size, use this(excluding the scrollbar size if there is):

var w = document.documentElement.scrollWidth;
var h = document.documentElement.scrollHeight;
dencey
  • 1,041
  • 17
  • 25