Page 1 of 1

iframe scrolling

Posted: Fri Apr 24, 2009 8:53 pm
by JellyFish
Is there a way to scroll to a specific position for an iframe of a separate domain with javascript? For example, could I move the scroll position to x:100, y:200 on an iframe that has the src attribute or "http://yahoo.com"?

Basically what I'm trying to do is clip any webpage to a specific position and dimension. Is there some way to do this?

I appreciate all help on this.

Re: iframe scrolling

Posted: Sat Apr 25, 2009 12:04 pm
by tech603
Yes this can be done by setting using the style attribute in the iframe for example something like below would set the position from the top border and the left border.
style="position:absolute;Left:15;Top:100">

Hope that helps

Re: iframe scrolling

Posted: Sat Apr 25, 2009 12:13 pm
by kaszu
He is asking about the scrolling position, not iframe position.
Google returned me this

Re: iframe scrolling

Posted: Sat Apr 25, 2009 9:03 pm
by JellyFish
kaszu wrote:He is asking about the scrolling position, not iframe position.
Google returned me this
I'm not a member of experts-exchange.com (Btw, good thing they put a "-" inbetween experts and exchange. LOL).

Re: iframe scrolling

Posted: Sat Apr 25, 2009 9:30 pm
by Christopher
I googled and found this which I tested and it works (Mozilla). It sets the body margin, so I assume that you could scroll both direction, animate, etc.

Code: Select all

<html>
<head>
</head>
<body>
    <iframe id="iframe1" style="height: 200px; width: 200px;"></iframe>
    <script type="text/javascript">
    var t = window.document.getElementById("iframe1");
    t.src= "mypage.html";
    t.onload = function()
    {
        t.contentWindow.document.body.style.marginLeft = "-100px";
    }
    </script>
</body>
</html>

Re: iframe scrolling

Posted: Sat Apr 25, 2009 10:05 pm
by JellyFish
arborint wrote:I googled and found this which I tested and it works (Mozilla). It sets the body margin, so I assume that you could scroll both direction, animate, etc.

Code: Select all

<html>
<head>
</head>
<body>
    <iframe id="iframe1" style="height: 200px; width: 200px;"></iframe>
    <script type="text/javascript">
    var t = window.document.getElementById("iframe1");
    t.src= "mypage.html";
    t.onload = function()
    {
        t.contentWindow.document.body.style.marginLeft = "-100px";
    }
    </script>
</body>
</html>
This would work only if the "mypage.html" part was mine. I'm trying to do this cross-domain, which means I can't access the document object of the iframe. If I could access the document object of the iframe, I think I would just use window.scrollTo(x,y).

Re: iframe scrolling

Posted: Sun Apr 26, 2009 1:39 am
by Christopher
What if you created a script on your site that would proxy the pages your want (e.g., mypage.php?url=http://somesite.com/somepage.html)

Re: iframe scrolling

Posted: Sun Apr 26, 2009 2:12 am
by JellyFish
arborint wrote:What if you created a script on your site that would proxy the pages your want (e.g., mypage.php?url=http://somesite.com/somepage.html)
I guess I could do that, but are there any security risks involved? And what about bandwidth issues?

Re: iframe scrolling

Posted: Sun Apr 26, 2009 12:48 pm
by Christopher
If you always know what the remote pages will be then you can make it secure by not passing parameters and having "sitefoo.php" and "sitebar.php" or passing IDs for pages that you lookup to check if they exist like "site.php?id=foo"