0

When iframe is loading I use a css class 'active' when is stop loading I switch to 'inactive' class.

<script type="text/javascript">
function frameload(){
    document.getElementById("loadnav").className = "active";
}
document.getElementById("loadnav").className = "inactive";
</script>
<iframe src="https://www.wikipedia.org/" onload="frameload()"></iframe>

The problem is that after loading does not return to 'inactive'.

1
  • 1
    I think you have to remove the initial class Commented Mar 3, 2017 at 21:32

2 Answers 2

1

Couple things

  1. You don't have an id on your iframe so document.getElementById won't do anything

  2. There needs to be some listener for DOMContentLoaded because document.getElementById("loadnav").className = "inactive"; is trying to run before the iframe is actually on the page

iframe {
width: 100%;
height: 100%;
}
iframe.inactive {
border: 2px solid red;
}
iframe.active {
border-color: green;
}
<script>
function frameload(){
    document.getElementById("loadnav").className = "active";
}
function init(){
  document.getElementById("loadnav").className = "inactive";
  document.getElementById("loadnav").addEventListener('load', frameload);
}

document.addEventListener('DOMContentLoaded', init);

</script>

<iframe src="https://www.wikipedia.org/" id='loadnav'></iframe>

Sign up to request clarification or add additional context in comments.

Comments

1

are you tried to use jQuery. the instruction document.getElementById("loadnav").className = "inactive"; is executed after the loading the iframe

$('iframe').load(function() {
 document.getElementById("loadnav").className = "inactive";
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.