Reference window object containing iFrame

Hello,

I'm sure I'm missing something simple here.

Lets say I have a page with an iFrame in it, and I want (from the page) to scrolldown to the bottom of the iFrame.

To do this in the page I can just use window.scrollTo(0,9999); for example. Question is, how do I reference the window object
that contains the iFrame so that I can scoll that instead of the parent?

I know how to reference the iFrame with its id, but its the containing window that I want to apply scrollTo() to.

Missing something obvious here?

Thanks





darkhorse0Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

smaccariCommented:
If your IFRAME tag has a name attribute, for example "myFrame", you can reference it through : frames["myFrame"].
0
darkhorse0Author Commented:
Well, as I said above, I knwo how to reference the iframe, the question is referencing its window right?

The method scrollTo() applies to a window
0
smaccariCommented:
The frames collection contains window objects.
So, by using frames["myFrame"], you reference a window object and can use the scrollTo method on it.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

smaccariCommented:
For more precision, if you reference your object throught document.getElementById, you will point an HTMLElement, not a window.
By using the frames collection, you will have your window object (though in this case it is not the id but the name attribute you have to use).
0
darkhorse0Author Commented:
Ok, I tried using the following syntax, which did'nt work:

frames['myiframe'].scrollTo(0,9999);

document.getElementById('myiframe').scrollTo(0,9999);

The first does nothing, returning no error, the second returns "object does'nt support..."

if the frames collection contains window objects then I must be missing a simple step here?
0
darkhorse0Author Commented:
And this is the iFrame tag source:

<iframe src="iframescrollbottom.html" name="myiframe" id="myiframe" Scrolling="yes" ></iframe>
0
smaccariCommented:
This code works for me :

<iframe src="page.htm" name="myiframe" id="myiframe" Scrolling="yes" ></iframe>
<a href="#" onclick="frames['myiframe'].scrollTo(0,9999)">GO</a>

If it doesn't for you, maybe should you give more details on your page and/or browser used.
0
darkhorse0Author Commented:
Calling onclick like you have works for me too. I don't know why, but just putting

frames['myiframe'].scrollTo(0,9999);

inside script tags after the iframe tag does'nt work. I need this to happen as the page loads. Any ideas why?

Try it - this is my entire page:

<html><head></head><body>
<iframe src="iframescrollbottom.html" name="myiframe" id="myiframe" Scrolling="yes" ></iframe>

<script language="JavaScript">

frames['myiframe'].scrollTo(0,9999);

</script>

<a href="#" onclick="frames['myiframe'].scrollTo(0,9999)">GO</a>

</body></html>

Yours works, mine does'nt???

0
darkhorse0Author Commented:
Ahh... it dawns. The iframe has not yet had time too load, this works:

setTimeout("frames['myiframe'].scrollTo(0,9999);",100)
0
darkhorse0Author Commented:
And for the same reason so does this:

<body onload="gotobottom()">


function gotobottom(){
frames['myiframe'].scrollTo(0,9999);
}
0
smaccariCommented:
Yes that's it ;)
You cannot scroll your frame if it is not loaded.
You could also put the scroll instruction in the onload event for the iframe :

<iframe src="iframescrollbottom.html" name="myiframe" id="myiframe" Scrolling="yes" onload="frames['myiframe'].scrollTo(0,9999);"></iframe>
0
darkhorse0Author Commented:
Yes, originally I had it in the iframe becuase I could'nt make the refernecing work from the parent, but thats ok now thanks to your help.

BTW, any idea how to do something more clever than scrollto 999? Its not nice, can I have an anchor and set it to scroll to that?

I'll up the points since this is really a extra question
0
smaccariCommented:
Yes, you can put a named anchor at the bottom of the document in your IFrame.
Let's say you gave it the name "myAnchor", here is what you can do :

frames["myiframe"].document.anchors["myAnchor"].scrollIntoView();

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
darkhorse0Author Commented:
I had no idea about scrollIntoView()

Thanks for all your help smaccari, problems solved :)

Joe

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.