Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when an iframe starts to load new URL

How can I detect that an iframe on my page starts to load a new page?

Our situation is:

  • When iFrame content starts to change we need to display a "loading" animation and hide the search-box.
  • I know how to handle the "iframe has finished to load" event (to hide the animation) but not how to catch the initial "starting to change" event...

Note: I can attach jquery "click" hook to the links on the menu, which will work. However, inside the iframe content there are many cross-reference links, and the "change" event also applies for them! So we need to catch event when user clicks on a link inside the iframe or when the iframe src is changed via javascript - because we also want to show the loading-animation and hide the search-box.

like image 489
Philipp Avatar asked Jun 26 '13 08:06

Philipp


People also ask

How do I know if a website is loaded in iframe?

In short, to check if a page is in an iframe, you need to compare the object's location with the window object's parent location. If they are equal, then the page is not in an iframe; otherwise, a page is in an iframe.

Does iframe load asynchronously?

iframes should load asynchronously without any effort on your part.

What is iframe onload?

The onload event is used to run scripts and call JavaScript functions after the contents of a webpage have been loaded. It can be used to get the information about the user's browser so that the compatible version of the web page can be loaded. onload events are also sometimes used to deal with cookies.

How do I use iframe tags?

Definition and UsageThe <iframe> tag specifies an inline frame. An inline frame is used to embed another document within the current HTML document. Tip: Use CSS to style the <iframe> (see example below). Tip: It is a good practice to always include a title attribute for the <iframe> .


1 Answers

I found a better solution if iframe and the container page is same origin, don't have to put extra code into the inner page:

<iframe src="same-origin.com" onload="content_finished_loading(this)"></iframe> <script>     var indicator = document.querySelector('.loading-indicator');     var content_start_loading = function() {         indicator.style.display = 'block';     };      var content_finished_loading = function(iframe) {         indicator.style.display = 'none';         // inject the start loading handler when content finished loading         iframe.contentWindow.onunload = content_start_loading;     }; </script> 
like image 87
Bruce Avatar answered Sep 23 '22 18:09

Bruce