0

I have a set of control variables for my iframe history. It changes after the iframe is fully loaded (when the user clic on some link inside the iframe)

$('#iframe').load(function(){
    if ((action == 'forward')) {
        window.historyControl+=1;
    } else {
        window.historyControl-=1;
    }
});

For what I'm trying to do, it works fine BUT when the user changes the iframe url BEFORE the iframe fully load then the .load() function doesn't execute.

The same happens if I put that inside vanilla javascript and call the function with the onLoad=() parameters in the iframe.

Is there a way to execute some javascript code while the iframe load his contents?

I have no control over the iframe contents.

distante
  • 6,438
  • 6
  • 48
  • 90

2 Answers2

0

You can use $('#iframe').ready(function(){...}). The callback will execute when your iframe's DOM structure is ready, in contrast with .load() callback which executes only when images and such are loaded. Source: jQuery - What are differences between $(document).ready and $(window).load?

Community
  • 1
  • 1
  • Thanks, but it just works the first time the main page is loaded. But after the user clicks a links inside the iframe it doesn't capture the changed contents. – distante Aug 22 '16 at 13:40
0

Following the trace of @MarcosPérezGude, I added an script on top of the <head> in the iframe's php file. It triggers a function

<script>window.parent.myFunction();</script>"

And in the function on the parent window I execute my code

function myFunction() {
 if ((action == 'forward')) {
        window.historyControl+=1;
    } else {
        window.historyControl-=1;
    }
}
distante
  • 6,438
  • 6
  • 48
  • 90