Is there a way to include a javascript file only once or declare a function only once? The problem I am facing is that I have an HTML module containing javascript include. Well, this module is loaded into the loop, and therefore this file is downloaded several times. I have designed most of the kinks, but it bothers me that I know that the same function is created several times, and this view can be as many as 30 iterations. For me, I donโt like the fact that the same function is created over and over again. I do not care? Is there any way to prevent this? I know that I can detect when a function exists, can I put a function declaration between the if statement?
Update
I tried one of the suggestions:
if(typeof btnSendInvite_click != 'function') { function btnSendInvite_click() { alert("#invite_guest_" + $(this).attr("event_id")); return false; } }
but that will not work. I also tried
if(!btnSendInvite_click) { function btnSendInvite_click() { alert("#invite_guest_" + $(this).attr("event_id")); return false; } }
but that will not work. It happens that I have this line:
$(document).ready(function() { $(".btnSendInvite").bind("click", btnSendInvite_click); });
and when the button is pressed, these functions are executed six times, this is the amount of time that a file has been added that tells me that the function is created several times ... I think.
Update
So, after many problems, this problem turns into something other than what I thought. The binding is called several times, so it gets the binding several times and therefore calls the function several times. I think my next question is: is there a way to bind a function to a control only once? I already tried jquery "one" and it does not work.
source share