Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent location.hash in an iframe from scrolling the parent window in Chrome
    primarykey
    data
    text
    <p><strong>The scenario:</strong></p> <p>I'm building a kiosk app to run in Chrome. In it i'm loading an iframe from another domain into a modal via a link with an anchor (http:/www.whatever.com/#specific_content).</p> <p><strong>The problem:</strong></p> <p>When that content loads, the iframe jumps down to the #specific_content but the parent page also jumps.</p> <p>It's a single page kiosk app so the position of the parent page is very important. The actual details of the scenario are a bit more complicated than just the above:</p> <ol> <li>To inhibit scrolling in the app I have body {overflow:hidden;}</li> <li>To allow for positioning I use a wrapper container set to the size of the viewport.</li> <li>To simulate the scrolling I am either repositioning the wrapper absolutely, or positioning content within the wrapper absolutely.</li> </ol> <p>To demonstrate the problem I have set up a jsFiddle but you need to see the output without the jsFiddle chrome to see the full scope of the problem:</p> <p>Step 1) Click 'Link to content', that will reposition the wrapper</p> <p>Step 2) Click 'Link to load', that will load the iFrame content</p> <p>Demo: <a href="http://jsfiddle.net/w47GT/8/embedded/result/">http://jsfiddle.net/w47GT/8/embedded/result/</a></p> <p>Fiddle: <a href="http://jsfiddle.net/w47GT/8/">http://jsfiddle.net/w47GT/8/</a></p> <p><strong>The code:</strong></p> <p>CSS:</p> <pre><code>html, body { height:100%; padding:0; margin: 0;overflow:hidden; } .background { width : 100%; height:400%; background:#333 url('http://incurablystircrazy.files.wordpress.com/2012/04/runner-at-sunset.jpg'); } .wrapper { position:absolute;width:100%;height:200%; } iframe { width:50%;height:50%;position:fixed;top:25%;left:50%;margin-left:-25%;border:2px solid red;background-color:#ddd; } .link { width:100%;height:25px;position:absolute;top:25px;background-color:green; } .anchor { position:absolute;top:400px;width:100%;height:25px;background-color:red; } </code></pre> <p>HTML</p> <pre><code>&lt;div class="wrapper"&gt; &lt;div class="background"&gt;&lt;/div&gt; &lt;a class="link" href="#linked"&gt;Link to content&lt;/a&gt; &lt;a class="anchor" name="linked" href="http://www.smashingmagazine.com#footer" &gt;Link to Load iFrame&lt;/a&gt; &lt;/div&gt; &lt;iframe&gt;&lt;/iframe&gt; </code></pre> <p>JS</p> <pre><code>$(function(){ $('.link').on('click',function(){ $('.wrapper').css({top:'-400px'}); }); $('.anchor').on('click',function(e){ e.preventDefault(); var href = $(this).prop('href'); $('iframe') .attr({ src: href, name: 'testing' }); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload