Issue with SmartAdmin Template Pagefunction Being Executed Twice

While developing a web application using the smartAdmin Template, an ajax-based template that you can preview here, I encountered some challenges. The issue I faced was that when I wrote a JavaScript function on one page, it affected all pages.

For example:

$(document).on('click', '#elementA', function(){
 alert('Hello World');
});

This code snippet worked on other elements of pages with the same id. It became impractical to assign unique ids to every element due to the project's size and complexity, which I have been working on for six months. In my quest to find a solution, I attempted giving each page a unique id and modifying the script as follows:

$(document).on('click', '#pageA #elementA', function(){
   alert('Hello World');
});

Although this seemed to solve the initial issue, the function stopped working on other elements of different pages. Additionally, upon revisiting #PageA for the second time, the function ran twice. It appears that the template stores user-defined functions in local memory storage (my assumption, not confirmed) and continues to accumulate them until the entire template is refreshed.

Answer №1

Well, after extensive research and development, I finally managed to solve this issue on my own. I utilized the loadScript() function to prevent redundant script loading.

I consolidated all scripts into a single file (resulting in two view pages for one page).

Previously, it was structured like this: A.php -> JScript + PHP & HTML and now it looks like this: A.php -> PHP & HTML, script/A.php -> OnlyJS

Given that I'm utilizing the CodeIgniter framework and want to restrict access to JS by URL, this process proved to be effective.

Here's the code snippet from my view file:

loadScript("<?php echo site_url('processor/load_script/path_to_folder/script/add'); ?>");

And here's the function on the Processor controller:

public function load_script($path)
{
    $last_segment = count($this->uri->segment_array());
    $path = '';
    for($i=3;$i<=$last_segment;$i++)
    {
        $path .= '/'.$this->uri->segment($i);
    }
    $this->load->view('core/ajax'.$path);
}

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

Whenever I try to access my Node.js API from Heroku's live server, I encounter a CORS policy error

Whenever I try to access my Node.js API from the Angular app (running on a local setup) and host the API on Heroku's live server, I encounter the following error: CORS policy: No 'Access-Control-Allow-Origin'. Interestingly, when I host the ...

Tracking progress with a dynamic bar from the beginning to the end

Struggling to develop a progress bar that corresponds to project start dates and end dates. With a roster of 100 projects, spanning from mid-2017 to future launches, the aim is to depict progress between 2018, 2019, and 2020. If a project commenced in 2017 ...

Problem with autocomplete functionality in Angular Material's md-contact-chips

Having some trouble with the autocompletion feature of md-contact-chips. I want to capture the $query as soon as someone starts typing. HTML <md-contact-chips ng-model="members" md-contacts="querySearch($query)" md-contact-name="fullname" ...

Determine the specific cell involved in an HTML5 drag-and-drop interaction within a table

I've been experimenting with the HTML5 drag and drop functionality in an Angular project. Here's the setup I'm working with: A container containing draggable 'objects' A table where users can drop the dragged elements Following ...

What strategies can be implemented to effectively utilize both client-side and server-side validation without redundant efforts?

Currently, I am validating user input on the server side using PHP. However, the client side sends XMLHttpRequest calls and highlights invalid fields with red borders. While this method works well, I believe it can be time-consuming for users to wait for a ...

Pass the value of a button to jQuery AJAX using the onclick() function

Can a value be passed from a button to Jquery AJAX using the onclick function? I attempted to use this code, but it doesn't seem to be working. Nothing is showing up in the console log. HTML <button type="button" class="hapus_krs" onclick="hapus_ ...

My goal is to develop a custom forEach function that retrieves a substring from each element in an array

I have a file containing image names like: sciFi.png, posed.png, birdA.png, dolphin.png, horse.png, shake.png, loft.png, basement.png, avenger.png, friend.png In my JavaScript document, I am trying to read this file and convert it into an array. Then, I w ...

Guide on generating a route using coordinates in a threejs environment

Currently, I am working with an array of coordinates obtained from the Google Maps Navigation API. I have successfully plotted these coordinates on a sphere, however, my objective is to extract the shape of the path by combining all the coordinate points ...

Utilizing DOMStringMap data to populate CSS URLs

I am attempting to create a universal CSS modification for a webpage element that resembles: <button data-action="link" class="cardImageContainer coveredImage cardContent itemAction lazy lazy-image-fadein-fast" data-blurhash="lo ...

Choosing a Element following the Execution of a jQuery appendTo Method

My goal is to dynamically add a set of elements to a div using jQuery and then reference them. It's strange that even though I can see these elements in the DOM in firebug, jQuery doesn't seem to recognize them. Here is the HTML code for the div ...

Send the contents of a `<ul>` element to the server using AJAX for form submission

Can someone assist me in submitting a serialized <ul> list through an AJAX post form request? I need help with this process. Below is my current code snippet. HTML: <form id="update_fruit_form" method="post" action="/update_fruits" accept-charse ...

using vue.js to hide a div after a certain amount of time passes without the mouse hovering over it

Hello, I asked a similar question earlier but I've been stuck on this problem for quite some time. I have a div that is set to v-show on specific events, such as when hovering over div1 and clicking on div2. I want to make it so this div disappears a ...

Saving the image path to a variable with a PHP form using an Ajax post request is not working as expected

Greetings! I've been diligently working on a PHP form (index.php) that utilizes an Ajax request to send data to another PHP file (create_product.php), responsible for saving information to a MySQL database. However, I've encountered a roadblock w ...

Numerous Cycles Stemming from a Single Answer

My goal is to display a list of products with their details, including a sublist of product models that are checked in the detailed list. The challenge is to achieve this using only one GET request. This means retrieving the JSON data of products once and ...

Utilize AJAX to invoke a C# method in a website development project

I need some help with my web site project. I have a folder called app_code which contains a file named ConnectCrm.cs. Also, in the same project, there is a file named VoC.cshtml. I am trying to call a method from ConnectCrm.cs file using ajax in VoC.cshtml ...

Is a 'Virtual DOM' included in React Native's architecture?

According to the ReactJS wiki page on Virtual DOM: React uses an in-memory cache of data structures to efficiently compute differences and update the displayed DOM in the browser. This allows developers to write code as if the entire page is re-rendered ...

React's createRef() versus callback refs: Which one provides the ultimate edge in performance?

Lately, I've delved into React and grasped the concept of refs for accessing DOM nodes. The React documentation discusses two methods of creating Refs. Could you elaborate on when a callback ref is preferable to createRef()? Personally, I find createR ...

Styled Components do not have the ability to define :hover states

export const WatchVideoButtonWrapper = styled.div` position: absolute; bottom: 80px; right: 33px; background-color: ${(props) => props.bgColor}; color: ${(props) => props.color}; border-radius: 100px; transition: all 0.1s; ...

Examining current elements in database using Node.js and MySQL

There appears to be a problem with the code inside con.query(query, function (err, result, fields). The issue is that it is never being called. This part of the code is meant to verify that when a user signs up, the email they enter is not already in use. ...

Extracting data from a dynamic table structure

Currently, I am working alongside my backend developer to address some challenges with form submissions. The specific situation involves a table within an HTML form that consists of two columns and rows that can be dynamically added. Users have the ability ...