Scroll parent window to snap in iframe

Parent and iframe are in the same domain. Iframe has a dynamic height and does not have its own scrollbars - scrollbars are in the parent window.

Despite a great search, I can’t find a way to scroll the parent window to the anchor in the iframe when loading. those. iframe url - abc.com/iframe.html#link, but only the top of the iframe is visible when loading - the parent frame does not scroll to #link. Does anyone know how this can be done using javascript (either in the parent, in the iframe, or both) or otherwise?

thanks

+4
source share
2 answers

This can be done using javascript. Suppose you click on a link, not a binding anchor, try the following:

document.getElementById('id_of_link').scrollIntoView(true); 

Edit: how about window.parent.scroll (x, y), where x, y are the positions of the elements obtained using this method:

 function getOffset( el ) { var _x = 0; var _y = 0; while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) { _x += el.offsetLeft - el.scrollLeft; _y += el.offsetTop - el.scrollTop; el = el.offsetParent; } return { top: _y, left: _x }; } var x = getOffset( document.getElementById('yourElId') ).left; 

From: Get the position (X, Y) of an HTML element

+2
source

Have you tried urlencoding #?

Try it:% 23

0
source

All Articles