# IFRAME how to scrollTo position?



## liqourmaster

Hi. I am trying to get an inframe's scroll position with a scrollTo() script but it just scrolls my main window page to desired position.

here is the code I've been playing around with so far that I got off the site techmusing:



Code:


<script>
function loadorder() {
document.getElementsByName("iframename").src="http://www.megavideo.com/?v=I6LLA0QL"
window.scrollTo(35,100)
}
</script>

<IFRAME onload=loadorder() name="iframename" vspace="0" hspace="0" allowtransparency="true" scrolling="yes" marginwidth="0" marginheight="0" frameborder="0" style="border:0px;" width="476" height="526" SRC="http://www.megavideo.com/?v=I6LLA0QL"></IFRAME>

However, I found out the author of the article was not trying to get a default scrollTo for the IFRAME but the main window itself. So my question is, how do I do the same but for the IFRAME?
I've tried putting the script before closing the IFRAME but it seems scripts dont run in iframe. Basically I'm trying to get the default position of the Iframe to center in on videos, pictures, etc.
I'm a newbie so go easy on me guys. Thank you.


----------



## tomdkat

liqourmaster said:


> I'm a newbie so go easy on me guys. Thank you.


 First, you should understand the script you're using so you can tweak it to function the way you want or need if you're wanting to use it in a different way than it was intended to be used. 

With that being stated, let's look at your JavaScript:



Code:


<script>
function loadorder() {
document.getElementsByName("iframename").src="http://www.megavideo.com/?v=I6LLA0QL"
[b]window.scrollTo(35,100)[/b]
}
</script>

The line in bold above instructs the window object to scroll to the specified location. Since you want to scroll the iframe, try doing somthing like this:


Code:


function loadorder() {
theFrame = document.getElementsByName("iframename");
if (theFrame <> null) {
   theFrame.src="http://www.megavideo.com/?v=I6LLA0QL";
   theFrame.contentWindow.scrollTo(35,100)
}
else {
alert("could not get iframe element!");
}
}

You can get more information about this here.

Hope that helps! 

Peace...


----------



## liqourmaster

Hi. Thanks for the reply.
I tried your piece of code it still didnt work. I looked through the page you referred me to, tried changing the urls around but that didnt work either then I saw at the end of their post:

*Also note that the contents of the iframe must be on the same domain*.

So does that mean I cant do it with image and video sites not hosted by myself?


----------



## tomdkat

I managed to get the iframe loading in the manner you are wanting to be using getElementById() instead of getElementsByName(). After that, I wasn't able to get the frame to scroll as instructed. At least not in Maxthon 2.1.1 (IE6 engine), Firefox 3, or Opera 9.5, all on Windows XP. Believe it or not, the iframe DID scroll in Safari 3.1.2 and I loaded Yahoo.com as the source of the iframe. Why Safari scrolled and the others didn't, I don't know.

So, you might be SOL on doing what you're wanting to do.

Peace...


----------



## liqourmaster

alright. thanks tom. do you know any other sites specializing in html? I use this site for all my tech needs  I'll try asking around other sites and I'll keep you posted. You're probably right though I'm probably SOL.
thanks for your help.


----------



## tomdkat

If you do a search on "iframe scrollTo", you'll find plenty of other forums where they discuss HTML.

Alternatively, I've had good success on the Opera forum in the "cross-browser web design with Opera" forum and I've had good success on the MozillaZine forum in the "web standards / evangelism" forum.

Good luck!

Peace...


----------

