javascript - On the first load of my site, why does it redirect the the page I access with XMLHttpRequest? - Stack Overflow

In the script at the bottom I get the result from phpgetitems-sold.php and put it into the div calle

In the script at the bottom I get the result from /php/get/items-sold.php and put it into the div called items-list. It's not supposed to actually go to the page /php/get/item-sold.php, but on the FIRST load of my site it does - or it goes to the banner php file or the sidebar php file which are loaded in sidebar.js. But on subsequent loads it works entirely like it's supposed to.

I just tried putting the code that loads it in an event listener for DOMContentLoaded, as you can see in my code below, but it fixed nothing.

What could be causing this and how can I fix it?

<!DOCTYPE HTML>
<html>
    <head>
        <title>Crochet Store</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
        <link rel="stylesheet" href="/assets/css/main.css" />
    </head>
    <body class="is-preload">
        <script>
            // to eliminate Flash Of Unstyled Content
            const loadingOverlay = document.createElement("div");
            loadingOverlay.id = "loading-overlay";
            loadingOverlay.innerHTML = "<div class='lds-ring'><div></div><div></div><div></div><div></div></div>";
            document.getElementsByTagName("body")[0].prepend(loadingOverlay);
        </script>
        
        <!-- Wrapper -->
            <div id="wrapper">

                <!-- Main -->
                    <div id="main">
                        <div class="inner">
                            <!-- Header -->
                            <!-- sidebar.js will fill this -->
                                <header id="header"><p id="js-disabled-warning">You have javascript disabled... the site needs javascript to function. Please enable javascript :(</p></header>

                            <!-- Banner -->
                            <!-- sidebar.js will fill this -->
                                <section id="banner"></section>

                            
                            <!-- Section -->
                                <section>
                                    <header class="major">
                                        <h2>Items I Sell:</h2>
                                    </header>
                                    <div id="items-list"></div>
                                </section>

                        </div>
                    </div>
                    <!-- sidebar.js will fill this -->
                    <div id="sidebar"></div>

            </div>

        <!-- Scripts -->
            <script src="/assets/js/jquery.min.js"></script>
            <script src="/assets/js/sidebar.js"></script>
            <script src="/assets/js/util.js"></script>
            <script src="/assets/js/main.js"></script>
            <script>
                function resizeBannerImg() {
                    const width = document.getElementById("banner-img").clientWidth;
                    $("banner-img").attr("height", width);
                }

                window.addEventListener("DOMContentLoaded", function() {
                    xmlhttp = new XMLHttpRequest();
                    xmlhttp.onload = function() {
                        const response = this.responseText;
                        $("#items-list").html(response);
                    };
                    xmlhttp.open("GET", "/php/get/items-sold.php");
                    xmlhttp.send();
                });
            </script>
    </body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745655016a4638491.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信