Script and Ajax loading repeatedly, causing multiple instances of script and Ajax loading

I recently encountered some unexpected issues while debugging and playing around with the code on my website, . Let me walk you through the scenario to understand the problem better. When you click on the "art" tab, two logs appear in the console - one showing a script length of 5 and the other an ajax call of 1. However, I only have 4 known scripts in total. Moving on to the next tab, "music", the script length log shows 8 twice, along with two ajax calls as well. This pattern continues with each subsequent tab. I am seeking assistance not in the form of writing code for me but rather guidance in identifying and rectifying my mistakes so that I can improve my JavaScript skills. I am open to discussing the issue further and working collaboratively to resolve it. Here is a snippet of my code (please ignore the script check function meant for commenting out):

*For more detailed code, please visit my website at

function handlersAttached(){
  $('.header a').on('click', ajaxLoad);

}

handlersAttached();


function ajaxLoad(e) {
  console.log($("script").length);
  console.log("ajax called");
    e.preventDefault();
    var url = $(this).attr("href");
    var that = $(this);
    that.off('click'); // remove handler
    $.ajax({
      url: url,
      dataType: "html",
      type: "GET",
      cache: false
    }).done(function(data){
        $("#container").html(data);
    })
};

Answer №1

Upon the initial click, $(scripts) yielded the following tags:

<script async="" src="https://www.google-analytics.com/analytics.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="../assets/instafeed.js-master/instafeed.min.js"></script>
<script type="text/javascript" src="../assets/load_scripts.js"></script>
<script>/*google analytics stuff*/</script>

Subsequent clicks resulted in the following:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="../assets/instafeed.js-master/instafeed.min.js"></script>
<script src="../assets/load_scripts.js"></script>
<script async="" src="https://www.google-analytics.com/analytics.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="../assets/instafeed.js-master/instafeed.min.js"></script>
<script type="text/javascript" src="../assets/load_scripts.js"></script>
<script>/*google analytics stuff*/</script>

It appears that the first three scripts are added to the DOM tree with every ajax call made. Additionally, load_scripts.js is included, leading to more ajax calls and triggered handlers with console.log being executed.

Solution: Avoid including scripts in the ajax response or refrain from appending them to the DOM tree. The choice depends on your specific goals :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I deactivate a checkbox after selecting another checkbox within an array?

Is it possible to automatically disable the checkbox labeled name="check_no[]" once the checkbox labeled name="check_yes[]" has been checked? <tr> <td colspan="" width="6%"> <center> <input type="checkbox" name="check_ye ...

iOS 10's autofocus feature experiencing difficulties in focusing on input

While using an application on my desktop or Android device, I have noticed that the input focus works perfectly fine. However, when I try to run the same application on iOS 10 Safari, the input focus does not seem to be working. It is worth noting that I ...

Finding distinct values in a multidimensional array using JavaScript

When working with JavaScript, I created a multidimensional array using the following code: result.each(function(i, element){ init.push({ label : $(this).data('label'), value : $(this).val(), }); }); The resulting array in ...

Tips for achieving a slow scrolling effect similar to the one displayed on these websites

I've noticed smooth slow scrolling on various websites and have been searching for React or Vue plugins to achieve this effect. However, I am interested in learning how to implement it using vanilla JavaScript. Feel free to suggest plugins, libraries, ...

jQuery - harnessing the power of JavaScript events such as "dragover"

I have a JavaScript function that includes an event listener for 'dragover'. Here is the code snippet: document.getElementById("someID").addEventListener("dragover", function(){ //Add your logic here }, fa ...

centering an angular material card on the webpage

Currently, I have developed a Registration script that enables users to Sign Up on my website. However, the issue I am facing is that the angular material card, which houses the sign up interface, is not centered. Despite trying various methods such as & ...

Adding some extra space inside a flex container using Bootstrap's padding

When attempting to use padding within a d-flex container to separate elements that are placed inline due to the d-flex class, I encountered an issue where the padding was not being applied and did not change anything. If I do not utilize the d-flex class, ...

The image source may vary, but the image itself remains the same

When I click on a button, the image should change but it's not happening in the browser even though I can see that the source code is being updated. How do I solve this issue? // AvatarGenerator.eyes = 1; moveRightBtn.on('click', function( ...

Authentication not being triggered by Adal.js

I have been attempting to incorporate adal.js into my application. Here is the code I am using. Can someone please help me understand why the authentication process is not being triggered? var app = angular.module('TestWebApp', [ 'ngRout ...

Issue with passing parameter values in MVC Html.ActionLink

Currently, I am experimenting with MVC for a demonstration and have managed to put together some components. However, I am encountering difficulties with an Html.ActionLink. The goal is to display a series of dropdown lists to the user that must be selecte ...

The power of Ng-show and filtering

What I am aiming for is to display a complete list of cities as soon as the page is loaded. Once a user clicks on a checkbox next to a city, my goal is to utilize ng-show/ng-hide in order to display the results specific to that city while hiding those of ...

Iterate through the call feature repeatedly, ensuring that each call has a different iteration number assigned to a variable within the

I have a situation where I need to call a certain feature (which has validations) multiple times within a loop. Currently, my code successfully calls the feature 3 times. * def xxx = """ function(times){ for(i=0;i<times ...

Arrange the div elements in a single horizontal line

Is there a way to align the company logo div next to the name, address, email, and phone number containers? I've attempted using display: inline and float: right, but the logo isn't moving up as desired. Here's the code snippet: http://jsfi ...

Kendo's data-bind onclick feature functions properly on web browsers but fails to work on mobile devices

As a newcomer to Kendo and JavaScript, I may be missing something obvious... In one of my list entries, I have a simple call like this: <li style="margin: 0.5em 0 0.5em 0"> <a href="#transaction-details" data-bind="click: onB ...

Webmatrix - Retaining query parameters throughout the website's pages

After successfully implementing pagination on my search page, I ran into an issue where the query strings used to build the search query are lost when transitioning between pages. Is there a simpler way to retain these query strings, or will I have to delv ...

`How can I extract a JSON string from a URL using Javascript?`

Currently, I am attempting to retrieve a JSON string from the following URL: This URL displays the JSON location value as a string, an example of which is shown below: {"BMS":[{"id":"PR01","type":"prajurit","lat":"-6.253310","long":"107.156219"},{"id":"P ...

ajax causing issues with comments display on rails platform

Having trouble rendering comments using ajax in my rails app. The comments are only displayed after redirecting the page, even though they are successfully created. The issue seems to be with rendering comments using ajax and jquery! In my application.htm ...

Enhance Your Images with Hovering Icons

Is there a way to display an icon checkmark over the main image when a user hovers over it? The icon should appear in the top right corner of the image. <div> <img class="result-image" src="{{$property->photo}}"> <! ...

The dimensions of the image are not conforming to the div's size

I am experiencing an issue with a div that contains an image. The image is not respecting the size of the div and is overlapping it. .bs-col-md-6 { box-sizing: border-box; -ms-flex-positive: 0; flex-grow: 0; 0; */ -ms-flex-negativ ...

The process of updating a nested object property in Redux and React

Initially, the user object is established with properties such as name, color, and age using the SET_USER method. I need to modify the name property within the user object utilizing UPDATE_USER_NAME. However, despite trying a nested loop within UPDATE_USER ...