Forum Moderators: open
related to another problem I recently posted I have narrowed down my problem.
I have to measure the users browser window and need to do this on some pages that contain Iframes within them...
The code I have now works in Firefox, but for some reason IE does not like it when there is an Iframe within the <body> tag.
anyone have any ideas? thanks in advance for any advice, really stuck on this one!
=====
<html>
<head>
<title>Iframe - measure window test</title>
</head>
<body onload="checkRightPanel();">
<script language="javascript" type="text/javascript">
<!--
window.onresize= checkRightPanel;
function checkRightPanel() {
userWidth = document.getElementsByTagName('body')[0].clientWidth;
if (userWidth >= 800) {
alert("big measure");
} else {
alert("small measure");
}
}
//-->
</script>
<!-- iframe start -->
<iframe width="500" height="240" src="http://www.webmonkey.com">
</iframe>
<!-- iframe end -->
</body>
</html>
for example:
in Firefox or Netscape, when loading this page, upon resizing the window the alert box will always tell you whether you are in a 'small size' or 'big size' (assessing if the browser greater of less than 800px wide).
when the same page is loaded into IE, it does not respond. I have tested this and it is only when the iframe is present in the code.
this is a dumbed down version of what I am trying to achieve, but this is the problem I have run into.
cheers...
after testing some url's in the source for the iframe I am getting mixed results in IE (Firefix etc are still fine)...
about 50/50 for success depending on the url that is provided for the iframe content.
does this make any sense? is the iframe pulling in any code that could possibly be conflicting with my javascript in IE?
cheers....
it's just the iframe seems to be interfering with my show div / hide div function (alert boxes in this test code...).
realistically, there is not going to be an iframe in all instances of this web page, but I know that there will be some. so I am trying to cover for these instances.
so I am using a staging base href for initial testing, and I realize this might not work. so I have gone back and played with different urls in the iframe src.
ex.
[webmonkey.com...] => doesn't work
[different.com.au...] => works fine
is this what you mean?
The page's onload event doesn't fire until everything has loaded, including the content of the iframe. It looks like - for whatever reason - your browser has a problem loading certain iframe content, and the
onload event never fires. We might as well work something out that doesn't rely on this event at all. The simplest way to do this is to call the function from within a small script block just inside the BODY (that way, we know the BODY exists before referencing it):
<script>checkRightPanel()</script> If the eventual script is also referencing other document elements, this script block could move to become the last element in the BODY instead.
This method has another advantage over using an
onload event listener. This is that the "initiation" procedure is called as soon as all the relevant elements are available, whilst an onload will wait for all content to download. If there are some heavy images this could be a few seconds later. This method does, however, break with the purist "separate behaviour from content" ideal. There is another method which allows all scripting to be kept in external files, but it involves running a cancellable loop that checks for the existence of elements before acting upon them. Maybe too much bother for purity's sake.
I removed the checkRightPanel() from the onload event and stuck it in the body as you suggested. It did not solve my error yet, but I did stick an alertMe() function in the onload event (just to check if the onload completed).
With the problematic urls the page does not completely download as you have said. The onload event never triggers.
So ideally the script:
<script>checkRightPanel()</script>
should cure that problem. but it still doesn't seem to for some reason.
Will try tomorrow with this at work in the live environment and let you know how it goes. Here's hoping it just fixes itself :-)
cheers!