Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Onload function crashes IE with IFrames

Status
Not open for further replies.

blfrd76

Programmer
Jul 17, 2003
34
0
0
US
I have one page with two IFrames. Here's the main page code:

<iframe id="largepic" src="#" name="lgpc"
onload="lgpcload(this)"
align="left"width="360"height="300" marginwidth=0 marginheight=0 hspace=10 vspace=0 frameborder=1>
</iframe>

The second IFrame contains a webpage of pictures. The first picture will serve as the SRC of the first IFrame.

Here's the function in a seperate .js file.

function lgpcload(obj){
if(obj.src == "#"){

obj.src = frames['nscroll'].document.links[0].href;
}
}

If I take out the IF statement, the page loads in an infinite loop and I have to manually stop IE, using CTRL+ALT+Delete.

Why does this cause IE to keep loading this page, over and over again?

Thanks,
bill






 
A translation of your code reads like this
Code:
As soon as this page loads
Check to see what the .src property is
if the .src property has the value "#" then
  load into this frame the url specified in the first link in the document occupying the 'nscroll' frame

Taking out the if statement means your code simply reads like:
Code:
As soon as this page loads
load into this frame the url specified in the first link in the document occupying the 'nscroll' frame

So, by taking the if statement out, the browser loads the first link in the 'nscroll' document every time the [tt]onload[/tt] even fires in the 'lgpc' frame.

It's important to note that the [tt]onload[/tt] event for an IFRAME is different to the [tt]onload[/tt] event of a document that might be loaded up in an IFRAME.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Ok. This link displays a picture. It's not a page with an .html extension.

frames['nscroll'].document.links[0].href;

So, the actual document loaded into the lgpc frame is really just a picture.

What I want to do is to be able to take out the IF statement.



 
OK, regardless of whether you are loading a HTML document, an image, a PDF or any other resource into your IFRAME, the [tt]onload[/tt] event of the IFRAME still fires when that resource is loaded - every time a resource is loaded in that IFRAME.

So even though you are loading up a static picture in the IFRAME, the IFRAME itself triggers the [tt]lgpcload[/tt] function when the picture has fully loaded. Then when you click on one of your links in the other page (to load up a new image in the IFRAME) it will fire again as soon as that resource has loaded. (Hence my point earlier regarding the difference between [tt]iframe.onload[/tt] and [tt]document.onload[/tt].)

I guess the solution lies in what you actually want to do in terms of the functionality of your application (there being a number of ways of skinning this particular cat).

My interpretation of what you've posted so far leads me to assume that, when the frameset loads, you want the [tt]lgpc[/tt] frame to be pre-populated with the image specified by the first link in the [tt]nscroll[/tt] frame... Am I on the right track?

If so, and you're dealing with a static list of images, so you know at this point which image will be the first one in the list, why not do away with the script altogether and simply specify a [tt]src[/tt] attribute for the frame:
Code:
<iframe id="largepic" 
        [b]src="first_image.jpg"[/b] 
        name="lgpc"
        align="left"
        width="360"
        height="300" 
        marginwidth=0 
        marginheight=0 
        hspace=10 
        vspace=0 
        frameborder=1 />

Alternately, if you are dealing with a fluctuating list that you can't predetermine the first image, why not tackly the problem in reverse. When the list loads up, populate the IFRAME with the image specified by the first link.
Code:
function lgpcload2(objBody){
  frames['lgpc'].src = objBody.links[0].href;    
}
And instead of calling it from the IFRAME, call it from the BODY tag of the list you're loading up in the [tt]nscroll[/tt] frame.

Just out of interest... What's so offensive about an if statement?

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top