最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far