0

I have an iframe embedded in a page. They are both on different domains but I have access to both.

I want to scroll to the top of the parent window when a link is clicked in the iframe. Somthing like this:

Within the Iframe page.

window.parent.ScrollToTop(); // Scroll to top function

On The parrent page:

window.ScrollToTop = function(){
  $('html,body', window.document).animate({
    scrollTop: '0px'
    }, 'fast');
  };

Is this even possible?

  • The Same Origin Policy prevents you from accessing the windows cross-domain to call any such methods. The way to do this is to use `postMessage` to have the iframe send a message to the parent, and then the parent can scroll itself. – CBroe Mar 21 '17 at 12:16

1 Answers1

0

If you have access to parent domain then you can use this:

window.top.scroll(0,0)

or if you want some animation use as:

$(window.top.document).find("body").animate({
scrollTop: 0
}, 600);
Nitesh
  • 1,490
  • 1
  • 12
  • 20
  • I have access to both but they are on different domains. Therefore when I try and call window.parent it gives an error about cross-domains. – user7742351 Mar 21 '17 at 11:45
  • yes, it will show because Javascript doesn't allow to change any behavior of one domain through some other domain. For more info, please check this: http://stackoverflow.com/questions/4724904/how-to-change-style-of-iframe-content-cross-domain – Nitesh Mar 21 '17 at 11:47