7

I have a problem when I try add event scroll with iframe tage. generally, I use scroll event with div tag It was working well. but when I add scroll event in iframe tag to detect user scroll pdf page, It was not working. why cannot I access html elements in iframe?, I have code inspect below:

enter image description here

and I try to add javascript scroll event with iframe :

HTML Code:

<iframe id="myframe" src="doc.pdf"></iframe>

JavaScript Code:

document.getElementById('myframe').onscroll = function(){
   alert('scrolling page');
};
DevScripts
  • 163
  • 1
  • 3
  • 6

3 Answers3

12

An iframe does not have a scroll method, the document of the iframe does - you need to reference the inner document rather than your <iframe> tag.

You can reference it with iframe.contentDocument and add a scroll even listener:

iframe.contentDocument.addEventListener('scroll', function (event) {
  console.log(event);
}, false);

Edit August 2023:

Reading the contents of an iframe is subject to the Same Origin policy, so this method only works if the iframe content is part of the same protocol and URL as the host document. Otherwise, iframe.contentDocument will return null. This is defined in the specification.

skyline3000
  • 7,639
  • 2
  • 24
  • 33
10

JavaScript provide us onscroll as an attribute which passes parameters (Can be a function). But we got iframe here, try the following code snippet (jQuery).

$("#yourFrameId").load(function () {
     var iframe = $("#yourFrameId").contents();

    $(iframe).scroll(function () { 
        //your code here
    });
});
haMzox
  • 2,073
  • 1
  • 12
  • 25
  • what is different from contents() with contentDocument ? – DevScripts Apr 06 '17 at 19:48
  • 1
    Sure. The contentDocument property returns the Document object generated by a frame or iframe element only. Whereas contents() method allows us to search through the immediate children of these elements in the DOM tree and construct a new jQuery object from the matching elements. – haMzox Apr 06 '17 at 19:52
0

I have faced the issue and here's one of my implementation.

var currAgreementTab = this.get('parentController').get('currAgreement');
var contractContainer = Ember.$('div#marginContractContainer');
var iframe = contractContainer.children(currAgreementTab.element)[0].contentWindow;

if (iframe) {
  iframe.onscroll = function() {
    var scrolledHeight = Ember.$(iframe).height() === Ember.$(iframe.document).innerHeight() ? Ember.$(iframe).height() : Ember.$(iframe.document).innerHeight() - Ember.$(iframe).height();

    if (Ember.$(iframe).scrollTop()  === scrolledHeight) {
      var currAgreementTab = that.get('parentController').get('currAgreement');
      Ember.set(currAgreementTab, 'checkDisabled', false);
    }
  }
}
joshweir
  • 5,427
  • 3
  • 39
  • 59
  • 2
    From Review: Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Nov 07 '18 at 08:45