On Google Chrome (37.0.2062.122, OSX / Windows), an iframe with an URL containing a fragment causes the parent elements to scroll up.
It's only happening in Chrome (tested in Safari and Firefox).
Here's a Fiddle showing the issue: http://jsfiddle.net/wmz5cu1y/1/ (you have to click Run
twice.)
As you can see, the whole <body>
containing the iframe has scrolled up. The header is hidden because it's now above the window, and there is an unexpected blank space at the bottom of the page.
How to avoid this ?
Bounty offered for a non-js workaround or a simpler html document reproducing the issue.
As other answers have already mentioned, the issue here is specific to JSFiddle. I can show you that with ease by simply including the same code within a StackOverflow snippet on this question:
Simpler HTML document
This issue happens with every fragment URL passed into an
iframe
on JSFiddle. Because of this, we can replicate this issue simply be creating a separate JSFiddle demo and linking to that in ouriframe
.I've created this new JSFiddle demo here: http://fiddle.jshell.net/JamesD/k5vk2u9h/1/show/light/#test.
As you can see, the fragment here is
#test
. This issue is now replicable in a new JSFiddle demo using the following markup:The
height
andwidth
attributes are irrelevant as the issue still appears without them.JSFiddle demo.
A workaround
The other answer here suggests that the fix is to modify JSFiddle's own HTML in order to re-render the page correctly. This isn't a great solution as in a real world situation (i.e. when linking someone to a JSFiddle demo), they may not have the expertise to achieve this.
We can get around this by using JavaScript. As the new HTML document I've created is on the same domain (http://jshell.net) we can execute JavaScript within the
iframe
itself. Manually setting thelocation.hash
property will also trigger this issue, so what we can do instead is scroll theiframe
directly to the element we wish to focus on.Firstly, remove the fragment from the URL in the
src
attribute of ouriframe
:As mentioned above, the fragment I've used is an element whose
id
is "test". This means the easiest way to get our element is to select it by itsid
attribute:Here,
frame
is a reference to ouriframe
within our own document, andelem
is our#test
element. We can get our matched element's position by usinggetBoundingClientRect()
:This will give us a ClientRect object like the following:
With this we now know that our
#test
element is positioned 3034px from the top of ouriframe
. Using this, we can now scroll the page ourselves to this position usingwindow.scrollTo()
(where thewindow
of ouriframe
in this case is held in ourframe
variable):JSFiddle demo.
Note that this solution will only work if the
iframe
points to a URL hosted on the same domain or if CORS has been enabled on the page theiframe
is embedding.The best option
After doing all this though, the best bet is to simply notify the developers of JSFiddle that this is a bug. I've raised this as an issue on their GitHub repository here: https://github.com/jsfiddle/jsfiddle-issues/issues/547.
Reproducing script
Found a minimal way to reproduce the bug: http://jsfiddle.net/4oytgwon/1/embedded/result/ (you can see the bug happening after clicking on "load iframe").
It also happens outside of jsfiddle using the following code:
Revised Answer
There are two bugs being displayed here.
Chrome iFrame problem
When you run this, your window will scroll so that the fragment in the window is at the top of your page:
As you can imagine, this causes issues in certain fixed height websites like jsfiddle. It is also unique to Chrome (I've only tested on FF, not IE), Firefox doesn't scroll the parent window. There is a discussion about it not being in FF here.
Chrome iFrame parent scrolling fix
Fiddle
Add an onload event to the iframe that calls the following function. And don't use the fragment in the original:
As you can see in both the fiddle and the above snippet, the parent window no longer scrolls and the behavior is now the same for both FF and Chrome.
Source for fix.
Old Answer (alternate fix for jsfiddle)
Well its not only chrome , all browsers will have the same problem, seems like the site has heavy js scripting , no idea how but it does affect on the site you put iframe on, the easiest way solving it is removing the #up_8360397 from the link. Second way would be disabling javascript inside the iframe. but than you won't be able to auto scroll it down to the post you wanted, u'll have to set scroll positions manually.
The problem is not to include # in a iframe url, if you try with the next url in your example you will see there is no scroll: http://www.w3.org/TR/scxml/#scxml
So, I reviewed the page that you are showing to see if there is something weird: https://news02ycombinator02com2.mentionusercontent.net/item?id=8357207#up_8360397
If you check the JS it has something like the next:
There is a function called "scrollTop" which I think is a good candidate to investigate.
If you check the code a little bit more you will see that in some point the JS is getting the offsetTop and the offsetParent:
Conclusion: the issue is with the page that your are trying to show in your iframe, not with fiddle nor Chrome or # in the urls.
I hope this help.
EDIT
After reviewing the wiki example I see that is some js in the fiddle (so the first answer is right).
To avoid this with js you just include this:
Example:
http://fiddle.jshell.net/xbnt3Lu6/21/
I will try to review which js is causing the problem.