Running code just before </body> instead of waiting for DOM ready/DOMcontentloaded event
Date : March 29 2020, 07:55 AM
To fix the issue you can do You should put your JavaScript code in the place that makes the most sense for what it needs to do. In most cases, you need your js to attach events to DOM objects, which is hard to imagine if those DOM objects don't exist when the js is running. So putting it at the bottom of the html is a sensible, simple, and common approach. Is it the best? That's arguable.Another approach is to attach your JavaScript to the various events that different browsers fire when the DOM is fully loaded. There is nothing wrong with this, although detractors don't like that it's often done in a way that requires an additional blocking HTTP request in the head element.
|
DOMContentLoaded event not firing
Date : March 29 2020, 07:55 AM
I hope this helps you . The reason is that you inject the javascript in the webViewDidFinishLoad: method. That method is called after the UIWebLoad has loaded the HTML page. Meaning that the DOMContentLoaded event may already have been fired, before your javascript has been injected. I see two possiblities how to fix this problem:
|
Should I wait for DOMContentLoaded event if I place a script tag at the end of a body tag
Date : March 29 2020, 07:55 AM
it helps some times Basically no. If script modifies element, it needs to exist. If you put script after that element it exists. <div id="myid"></div>
<script>
getElementById('myid'); // it will work, Element myid Is defined
<script>
<script>
getElementById('myid'); // it will fail, it's not yet defined, it will be
// Using DomContentLoaded here would be requrired
<script>
<div id="myid"></div>
|
unpoly links attribute to load the link's url on DOMContentLoaded event
Date : March 29 2020, 07:55 AM
I hope this helps you . Wouldn't following a link on load replace the page you just loaded? Anyway, this would be a way to activate a link as soon as it enters the DOM: up.compiler('a.auto-follow', function(link) {
up.follow(link)
})
<a class="auto-follow" href="...">...</a>
|
Universal rendering creates a delay between DOMContentLoaded event to Load event
Date : March 29 2020, 07:55 AM
|