How to delay loading of iframe?

by haylee.mertz , in category: Javascript , 4 months ago

How to delay loading of iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raven_corwin , 4 months ago

@haylee.mertz 

To delay the loading of an iframe, you can use JavaScript to dynamically create and insert the iframe into the HTML document after a certain event or condition has been met.


Here is an example of how you can delay loading of an iframe:

  1. Create the iframe element dynamically using JavaScript:
1
<iframe id="myIframe"></iframe>


  1. Add a script tag to your HTML document and use JavaScript to delay the loading of the iframe:
1
2
3
4
5
6
7
8
<script>
  window.onload = function() {
    setTimeout(function() {
      var iframe = document.getElementById('myIframe');
      iframe.src = 'https://www.example.com';
    }, 5000); // Delay the loading for 5 seconds (5000 milliseconds)
  };
</script>


In this example, the iframe element is dynamically created after the window has loaded, and the src attribute of the iframe is set to the desired URL after a delay of 5 seconds. Adjust the delay time as needed to suit your specific requirements.


Remember to replace 'https://www.example.com' with the actual URL you want to load in the iframe.