0

I have a script that appends several iframes (8 as an example) into the body:

image_iframe[sequential number 1-8] = $('<iframe />');
image_iframe[sequential number 1-8].appendTo('body');

When I try to stop images in specific iframes from loading:

if(window.stop !== undefined) {
      image_iframe[2].contentWindow.stop();
}
else if(document.execCommand !== undefined) {
      image_iframe[2].contentDocument.execCommand("Stop", false);
}

I get the following error:

Uncaught TypeError: Cannot read property 'stop' of undefined

How can I fix this?


Here's a jsfiddle with the original code. Just click the 'Next' button a few times to reproduce the error. It is based on this jsfiddle from this question which works fine.

I did some searches and found these two questions but not sure if it's the same problem:

  1. I get an undefine error in iframe
  2. how to correctly stop the transport inside an iframe
Community
  • 1
  • 1
Ming
  • 744
  • 4
  • 13
  • 26

1 Answers1

0

image_iframe[index] is a jQuery element. To get contentWindow, you will need to get the iFrame DOM element first. Suggest change

// from
image_iframe[this - 3].contentWindow.stop(); 

// to
image_iframe[this - 3].get(0).contentWindow.stop();
closure
  • 7,412
  • 1
  • 23
  • 23
  • I did the change and while it did remove the error, it still doesn't run. The images are still being loaded (checking the network tab). Replacing frame.contentWindow with window.stop(); works fine but it stops all images not just the specified iframes. – Ming May 07 '14 at 13:34