How to make an event only load once javascript


Hi, I'm doing a web with jquery and c #.

The problem I have is that when I click on menus on the web more than once, I think the html content of the page is being loaded again and again. With this, clicking on the menu repeats the click () event and clicks 2 times.

I would like to know how I can delete the previous script when loading the same html page again so that it does not end the page with the same script many times. Thanks.

The complete code is the following: Top Menu

          $(function () {
                $("[id*='gestion_polizas']").click(function () {


                    //por si tarda al cargar cuando se pulsa el boton
                    //setTimeout($("[id*='contenido']").load("JqGridInformatica.aspx"), 2000);

When clicking on a row in the grid (opens the side menu)

   onSelectRow: function (row_id) {

                          //hago un hide en el formulario anterior para crear el efecto
                          //de cargar e la izquiera de nuevo

                          //llamo a la funcion que le da datos al formulario a partir del key del jqgrid
                          //cargo el menu lateral
                          //muestor el menu lateral con efecto

Side menu that opens when you click on a grid row

<div id="menuPolizass">
<div id="listView" class="list listView">
            <li class="<%--list-item-active--%>"><a href="#" id="menuLateral_cliente" class='target listView'>Cliente</a></li>
            <li><a href="#" id="menuLateralPolizas_recibos" class='target listView'>Recibos</a></li>
            <li><a href="#" id="menuLateralPolizas_siniestros" class='target listView'>Siniestros</a></li>
            <li><a href="#" id="menuLateralPolizas_tarificacion" class='target listView'>Tarificación</a></li>
            <li><a href="#" id="menuLateralPolizas_imprimir" class='target listView'>Imprimir</a></li>

<div id="flotantePolizas"></div>

      function funciones_MenuLateral() {

          $("#menuLateralPolizas_recibos").click( function () {
              if ($("#jqGridRecibos").length == 0) {

                      width: 1200,
                      height: 800,
                      beforeClose: function (event, ui) {
                          //al cerrar el dialog borro el jqgrid para que no haya error al volverlo a abrir

                      "closable": true,
                      "maximizable": false,
                      "minimizable": true,
                      "collapsable": true,


                  //return false;
                  $("[id*='menuLateralPolizas_recibos']").attr('id', 'NEWID');

              else {
                  alert("Esta ventana ya esta abierta");



asked by wuasaa 17.03.2017 в 15:53

3 answers


Hello I see that you use jQuery, in this case the solution is very easy, due to the length of your code I will give you the example but the solution is the same using

.one() //de jQuery

$("#menu").one("click", function (){
     //esto solo se ejecuta la primera vez que das click en el elemento con el id menu
     alert("abrir menu");
  background: #000;
  color: #fff;
  min-width: 70px;
  width: 70px;
  max-height: 100%;
<script src=""></script>
<div id="menu">

My snippet what it does is simple, you click and only for the first time will launch an alert, What you will do is change your code every time you add the event click only in the places you want this to be executed once , Example:

$("[id*='gestion_polizas']").click(function () {

This is changed to:

$("[id*='gestion_polizas']").one("click",function () {
answered by 27.06.2017 / 08:50

The best thing would be if you did not bindate the event on that element twice, but well, if necessary, you could do the following:

$( "#foo").unbind( "click" ).click(function (){
 //Tu código

This will cause that when bindear the event click on the element, the one that previously existed will be removed before bindear the new one.

answered by 17.03.2017 в 16:13

Ok, so I just solved it, it was not a problem that the click event was loaded twice, the problem was that it reloaded the divflotante and gave error. What I've done is make a # divflotante.remove each once I call the function functionsmenralral () and so it works correctly

answered by 17.03.2017 в 16:41