admin管理员组

文章数量:1427320

I open a iframe after the page is fully loaded with this javascript code:

<script>
    function loadFrame() {        
    var iframe = document.getElementById("defframe");
    iframe.src = "/frame.php"
};
window.onload = loadFrame;
</script>

HTML Code

<iframe id="defframe" frameBorder="0" scrolling="no" src="about:blank" />   </iframe>

This works perfect but I have to load this iframe 10 seconds after the page is loaded.

1. wait for window.onload

2. If window.onload is ready, wait 10 seconds more before fire.

I tried:

<script>
    window.onload = function loadFrame() {        
    var iframe = document.getElementById("defframe");
    iframe.src = "/frame.php"

    setTimeout(defframe, 10000)
};   
</script>

Without success. Thank you very much

I open a iframe after the page is fully loaded with this javascript code:

<script>
    function loadFrame() {        
    var iframe = document.getElementById("defframe");
    iframe.src = "/frame.php"
};
window.onload = loadFrame;
</script>

HTML Code

<iframe id="defframe" frameBorder="0" scrolling="no" src="about:blank" />   </iframe>

This works perfect but I have to load this iframe 10 seconds after the page is loaded.

1. wait for window.onload

2. If window.onload is ready, wait 10 seconds more before fire.

I tried:

<script>
    window.onload = function loadFrame() {        
    var iframe = document.getElementById("defframe");
    iframe.src = "/frame.php"

    setTimeout(defframe, 10000)
};   
</script>

Without success. Thank you very much

Share Improve this question edited Apr 5, 2017 at 7:26 labu77 asked Apr 5, 2017 at 7:10 labu77labu77 8373 gold badges14 silver badges36 bronze badges 2
  • Why wait 10 seconds? – StackSlave Commented Apr 5, 2017 at 7:30
  • Because when the Iframe is loading, its not possible to click any link on the website until the content in the iframe is fully loaded. When the frame load first after 10 seconds. The User is reading and the probability that the user want click a link during the frame is loading is a bit more less. – labu77 Commented Apr 5, 2017 at 8:06
Add a ment  | 

4 Answers 4

Reset to default 1

The simplest way to do this is leave your function as it is, just call it in different way. Please follow below code:: HTML:

<iframe id="defframe" frameBorder="0" scrolling="no" src="about:blank" >   </iframe>

JS:

function loadFrame() {
    var iframe = document.getElementById("defframe");
    iframe.src = "/frame.php"
};
window.onload = setTimeout(loadFrame, 10000);

Working JSfiddle code:: https://jsfiddle/rnrztx1d/1/

We have to put code inside the set Time out method.

HTML:

<iframe id="defframe" frameBorder="0" scrolling="no" src="about:blank" />   </iframe>

JS

window.onload = function loadDefFrame() {
 setTimeout(function () {
 loadFrame(); //call your method
},10000)};

Complete Code:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<script>

  window.onload = function loadDefFrame() {
   setTimeout(function () {
   loadFrame(); //call your method
  },10000)};

  function loadFrame() {        
   var iframe = document.getElementById("defframe");
   iframe.src = "/frame.php"
  };

</script>
<body>

<iframe id="defframe" frameBorder="0" scrolling="no" src="about:blank" /></iframe>

</body>
</html>

You can do like this,

window.onload = function loadFrame() {        
    setTimeout(function(){
          var iframe = document.getElementById("defframe");
          iframe.src = "/frame.php"

    }, 10000)
};  

in window.load you can set up setTimeout function for 10 seconds.

Alternative way,

window.onload = function() {
  setTimeout(loadFrame(), 10000);
};

function loadFrame() {
  var iframe = document.getElementById("defframe");
  iframe.src = "/frame.php"
}

You need to wrap onload process in setTimeout

window.onload = function loadFrame() {
    setTimeout(function () {
        var iframe = document.getElementById("defframe");
        iframe.src = "/frame.php";
    }, 10000);
};

本文标签: javascriptWindowonload and run after x secondsStack Overflow