dimanche 31 octobre 2021

Execute Vanilla JS Scripts in React after DOM Render

I'm struggling my head for 5 hrs to get out of this issue, I've put some <script> tags in the index.html file that resides in the public folder. I want them to execute after the Browser has rendered the HTML completely, I know this is very basic question, but the problem is, I'm converting an HTML template to React, and it heavily uses animations, which I can't afford to write in react again from start. Here is my code

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Galaxy App</title>

    <!--CSS-->
    <link href="asset/css/icofont.min.css" rel="stylesheet" />
    <link href="asset/css/linearicons.min.css" rel="stylesheet" />
    <link href="asset/css/magnific-popup.min.css" rel="stylesheet" />
    <link href="asset/css/animsition.min.css" rel="stylesheet" />
    <link href="asset/css/bootstrap.min.css" rel="stylesheet" />
    <link href="asset/css/swiper.min.css" rel="stylesheet" />
    <link href="asset/css/styles.css" rel="stylesheet" />
    <link href="asset/css/main.css" rel="stylesheet" />
    <link href="asset/css/homepage.css" rel="stylesheet" />


    <!--Theme CSS-->
    <link href="asset/css/theme.css" rel="stylesheet" />
    <link href="asset/css/responsive.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.js"></script>

    <script src="asset/js/polyfill.min.js"></script>
    <script src="asset/js/jquery.min.js"></script>
    <script src="asset/js/jquery.viewport.min.js"></script>
    <script src="asset/js/jQuerySimpleCounter.min.js"></script>
    <script src="asset/js/jquery.magnific-popup.min.js"></script>
    <script src="asset/js/animsition.min.js"></script>
    <script src="asset/js/isotope.pkgd.min.js"></script>
    <script src="asset/js/bootstrap.bundle.min.js"></script>
    <script src="asset/js/rellax.min.js"></script>
    <script src="asset/js/swiper.min.js"></script>
    <script src="asset/js/smoothscroll.js"></script>
    <script src="asset/js/svg4everybody.legacy.min.js"></script>
    <script src="asset/js/TweenMax.min.js"></script>
    <script src="asset/js/TimelineLite.min.js"></script>
    <script src="asset/js/typed.min.js"></script>
    <script src="asset/js/vivus.min.js"></script>
    <script src="asset/js/main.js"></script>
    <!-- Theme JS -->
    <script src="asset/js/theme.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.js"></script>
    <script src="asset/js/cutsom.js"></script>
  </body>
</html>

I manually checked by logging the document.getElementById() in the last script, and it returns undefined/null because at that time the HTML is not rendered completely, even though I've put the scripts at the end of file. it works if I add setTimeout in my script, and it captures the elements perfectly.

So what should I do to only load all the scripts when my HTML has been rendered completely.




Aucun commentaire:

Enregistrer un commentaire