Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

How to scroll to top of a page with code that is within an Iframe that is within several divs

I've got some existing code.  What it needs to do is scroll to the top of the page when each new 'page' is navigated to - this is simply a matter of replacing the html within the Iframe, so can't do it on a body onload or something like that.  The current code calls some javascript when the navigation is done.  They try to do a window.scrollTo(0,0); and a window.parent.scrollTo(0,0);, but neither works because the code is so far down into the window - or at least that's what I think.  I think I need to access the div with id of top and then use scrollTo on it, but not sure.  Thanks for your help in advance.  Please see attached html to see the structure of this.  Let me know if you need more details.  Thanks, Faith

Current code in frame's javascript that doesn't work is this:

window.scrollTo(0,0);
if(window.parent!= null)
{
window.parent.scrollTo(0,0);
}
<HTML xmlns="http://www.w3.org/1999/xhtml">
<BODY id="ctl00_ctl00_ctl00_HtmlBody">
<FORM id="aspnetForm" name="aspnetForm" action="/go/myhealthcenter/symptomreview.aspx" method="post">
<A id="TOP" name="TOP"><DIV id="Global"><DIV id="MainBody"><DIV id="Content"><DIV class="ContentLeft"><DIV 
 
id="Article"><DIV id="ArticleBody"> 
<IFRAME class="MHCDemographicIframe" 
 
id="ctl00_ctl00_ctl00_BodyContentPlaceHolder_MainContentPlaceHolder_MainContentPlaceHolder_MHCFrame" 
 
src="https://blah.com/PatientPortal/english/ReviewOfSymptoms.aspx?UserName=faithern" frameBorder="0" width="630" 
 
height="1150"></IFRAME> </DIV></DIV></DIV></DIV></DIV></DIV></A></FORM></BODY></HTML>

Open in new window

0
faithern
Asked:
faithern
  • 3
2 Solutions
 
faithernAuthor Commented:
The javascript I am referring to is within the Iframe's aspx page...
0
 
Lolly-InkCommented:
The code below which you use works fine.
Check whether the code actually gets called. Put an alert after it.

window.scrollTo(0,0);
if(window.parent!= null)
{
window.parent.scrollTo(0,0);
}

Open in new window

0
 
faithernAuthor Commented:
I don't need an alert - I am debugging the IE process and I can see that it gets called...but it does NOT work - the page does not get scrolled to the top.
0
 
faithernAuthor Commented:
I figured it out.  Added an anchor at the top of my frame, and used the following code:
document.getElementById('toptest').scrollIntoView(true);

Still can't access main page, but top of frame is pretty good.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now