Online since 1990 Yes! I started with Gopher. I do modern Web Component Development with technologies supported by **all** WHATWG partners (Apple, Google, Microsoft & Mozilla)
When the component is loaded before the DOM is fully loaded, it will defer its connectedCallback to the next readystatechange event by attaching an event listener and returning early, but when the DOM has already loaded, it will jump over the early return and do its initialisation as usual.
This works in all three cases:
When the component is defined before the DOM finishes loading (via the event handler)
When the component is defined after the DOM finishes loading (skipping the early return)
When the component is inserted via javascript (again, skipping the early return)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
readystatechagewill not workHere is a full working JSFiddle: jsfiddle.net/WebComponents/d9sbzcex/
Extract of your solution:
Issues
(minor) You can not call
this.connectedCallbackin the handler, it will have the wrong lexical scope. so a.bind(this)is requiredBut you can't just run
connectedCallbackagain because it would run any other (initialization) code again also So I changed your code as aboveCode now runs fine
Although your workaround runs "late" after the
readystatechangeEventBut now another issue occurs
Because your code relies on an Event that happens only once, that code will never fire again.
So if you move DOM around, or append DOM, that Event will never fire.
Your component never did the "Access" again
That isn't my work-around though. Here's what that should look like:
When the component is loaded before the DOM is fully loaded, it will defer its
connectedCallbackto the nextreadystatechangeevent by attaching an event listener and returning early, but when the DOM has already loaded, it will jump over the early return and do its initialisation as usual.This works in all three cases: