-2

Hello I am using create element and my question is if the function can be executed when the page is loaded something like in jquery "document ready".

I am currently using this code which works when the user clicks on the div. It works for me for other purposes but now I need the function to run when the div loads or when the page loads.

My code:

s.a.createElement("div", { id: "iframe", style: { backgroundColor: "red" },
                                        onClick: function () {
                                        document.getElementById("iframe").innerHTML += "<iframe src='https://site.test' frameborder='0' id='iframe' style='display: block; overflow: hidden; border: none; height: 100vh; width: 100vw;'></iframe>";
                                        },
                                        },      
                                        ),

Poor changing onClick for OnLoad but without success.

I tried this but it gives me an error.

window.addEventListener('DOMContentLoaded', (event) => {
    console.log('DOM fully loaded and parsed');
});
s.a.createElement("div", { id: "iframe", style: { backgroundColor: "red" },
window.addEventListener('DOMContentLoaded', (event) => {
                                        document.getElementById("iframe").innerHTML += "<iframe src='https://site.test' frameborder='0' id='iframe' style='display: block; overflow: hidden; border: none; height: 100vh; width: 100vw;'></iframe>";
                                        },
                                        },      
                                        ),
NikoBoomer
  • 21
  • 5

2 Answers2

0
window.addEventListener('load', (event) => {
  console.log('page is fully loaded');
});

I think u meant that.

Redberry57
  • 36
  • 4
0

You are certainly looking for the DOMContentLoaded event.

window.addEventListener('DOMContentLoaded', (event) => {
    console.log('DOM fully loaded and parsed');
});

This fires once the HTML is available, but before images are loaded etc. Read more here

Salketer
  • 14,263
  • 2
  • 30
  • 58