1

I am trying to remove scrollbars from all iFrames on a page using jQuery.

I am using the code below for that:

$("iframe").each(function() { 
                         $(this).css({"overflow":"hidden","scrolling":"no"});
                    });

The problem is that as I keep refreshing the page sometimes it works and sometimes it doesn't which makes me believe there is some sort of a timing issue here. Perhaps the iFrames have not been loaded yet when the code above runs? Perhaps adding time delay?

The code is wrapped inside $(document).ready(function() handler

Joly
  • 3,218
  • 14
  • 44
  • 70

4 Answers4

0

Try this

$(document).ready(function(){
   $("iframe").each(function() { 
      $(this).load(function(){
             $(this).css({"overflow":"hidden","scrolling":"no"});
      }
    });
});

EDITED

Rickert
  • 1,677
  • 1
  • 16
  • 23
0

Setting the styles and attribute with javascript simply doesn't work at all, so your only option is to set it without javascript

<iframe src="http://url.com" style="overflow:hidden" scrolling="no"></iframe>

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

The problem is that you are probably loading the content of the iframes using javascript as well. since javascript is single threaded by design, the load of the content of the iframe will be delayed until after the rest of the javascript is executed.

You should add the function as a callback when you load the content in the iframe

A similar question has been answered here jQuery .ready in a dynamically inserted iframe

Community
  • 1
  • 1
Enermis
  • 679
  • 1
  • 5
  • 16
  • Makes sense, how do I do that? Can you give me an example please? – Joly Dec 12 '13 at 15:00
  • what user2348221 suggested was correct. for more info you can check here. http://stackoverflow.com/questions/205087/jquery-ready-in-a-dynamically-inserted-iframe – Enermis Dec 12 '13 at 15:03
0

Why use jQuery/JavaScript at all?

Simple CSS can turn it off

iframe {
    overflow: hidden;
}

No looping, no worrying if they are dynamic, it should just work. And if you need to enable it, make another rule with a class and turn on the scrolling.

epascarello
  • 204,599
  • 20
  • 195
  • 236