user3580096
user3580096

Reputation: 17

Iframe reloads and gives double content

I changed the content of an HTML5 iframe Tag with a function, however; the function reloads the whole page, which is an issue, as I am not intending for it to do that. Reloading the page in the iframe tag is duplicating the content. This is being caused because I am using the onload event trigger with the iframe tag. Is there a way to block reload in a function with doc.write => document?

Many thanks

iframe.onload = function() { 

function_insert_txt_iframe ...

}
window.addEventListener("DOMContentLoaded", function(event) { 

function_insert_txt_window ...

}

Upvotes: 0

Views: 171

Answers (2)

Kent Brewster
Kent Brewster

Reputation: 2520

Document.readyState seems like it might do what you want:

document.addEventListener('readystatechange', event => {
  if (event.target.readyState === 'interactive') {
    function_insert_txt_iframe();
  }
  else if (event.target.readyState === 'complete') {
    function_insert_txt_window();
  }
});

See here: https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState

Upvotes: 0

Melchia
Melchia

Reputation: 24294

You can use a variable called isLoaded and instantiate it with value false. If the value is true you don't need to reload the page:

var isLoaded = true;

iframe.onload = function() { 

    if(!isLoaded) function_insert_txt_iframe ...

}
window.addEventListener("DOMContentLoaded", function(event) { 

    if(!isLoaded)  function_insert_txt_window ...

}

Upvotes: 1

Related Questions