Jump to content

Code block only executes once

Recommended Posts

i have an embedded script on my site's homepage that uses JQuery's ready method to execute a function whenever I load my website. The code executes when I load the homepage the first time. But if I navigate to another page on my site and then come back to the homepage, the function won't execute again. How can I fix this?

Here's the code:

<div id="api_text">
  <script>
    $().ready(function() {
      $.get(
        "https://example_url_api.com/",
        function(data) {
          const results = JSON.parse(data.body);

          let print_string = "";
          for (var result_key in results) {
            const result_string = results[result_key];
            $("#api_text").append(result_key + " : ");
            $("#api_text").append(result_value + "<br/>");
            print_string += result_string;
          }
          console.log(print_string);
        }
      );
    });
  </script>
</div>

 

Link to comment
  • Replies 2
  • Views 440
  • Created
  • Last Reply

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.