I have an element inside an <iframe> I’d like to activate using a link outside the <iframe>.
Example:
<iframe src="iframe.html" id="iframe">
*inside the iframe*
<div id="activate" class="activate">
</iframe>
<a href="#activate" target="iframe">Link</a>
I thought this would work but obviously does nothing because I’m stupid. Any ideas?
Framed page (
test.html):Page containing the frame (
page-containing-frame.html):Explanation
nameattrbute with the value oftarget-iframe(obviously, you can choose any desired value).The link contains three attributes, each supporting two methods to scroll to a link in the frame:
target="target-iframe"andhref="test.html#activate"This is the fallback method, in case of an error occurs, or if the user has disabled JavaScript.
The target of the link is the frame, the
hrefattribute must be the path of the frame, postfixed by the anchor, egtest.hrml#activate. This method will cause the framed page to reload. Also, if the anchor is already at#activate, this method will not work any more.framesobject (by name, NOT by id,target-iframe). Then, the anchor is selected (document.getElementById('activate').Finally, the
scrollIntoViewmethod is used to move the element inside the viewport.The
onclickmethod ends withreturn false, so that the default behaviour (ie following the link, causing a page refresh), does not happen.Your current code did not work, because of the missing
nameattribute (target="..."cannot match IDs, only names). Also,#activateis parsed in the context of the current page, so, the link points topage-containing-frame.html.