1

I have the following jQuery source (modalConnect()) for loading a form and presenting it via a boostrap modal. It basically prevents the default behavior of an href tag and instead submits an ajax get call. This works fine.

Now I have to add another .js script tag to the dom which I receive from the server. This also works for the first click, for each succeeding click the method is executed the number of times it has been already executed + 1. Which is very strange.

So for the first click I receive the following in the console:

function executed Additional JS executed

which is fine.

For the second click (on the same or another - makes no difference) I receive the following:

function executed Additional JS executed

function executed Additional JS executed

Having now printed the text in total three times in the console, whereas I would only expect having printed it two times.

<script type="text/javascript">
    function modalConnect()
        {
            $(".editItem").click(function(ev) { // for each edit item <a>
                ev.preventDefault(); // prevent navigation
                var url = ($(this)[0].href); //get the href from <a>
                $.get(url, function(results){
                  var itemForm = $("#ajax_form_modal_result", results);
                  console.log("function executed");
                  //update the dom with the received results
                  $('#itemFormModal').html(itemForm);
                  //add a new js script to the document. Repeated method calling effect only occurs if I add the following two lines
                  scriptText = "console.log('Additional JS executed')";
                  appendScriptTag(scriptText);
                  //show a bootstrap modal with the loaded form
                  $("#itemFormModal").modal('show');
                }, "html");
                return false; // prevent the click propagation
            })
        }
</script>

asdf

 <script type="text/javascript">
    function appendScriptTag(scriptText)
    {
        //create a script tag and append it to the dom
        var script = document.createElement( 'script' );
        script.type = 'text/javascript';
        script.text  = scriptText;
        //the following lines seem to have no effect.
        //the "method called more often" effect is not influenced by these lines
        document.body.appendChild(script);
        document.body.removeChild(document.body.lastChild);
        delete UnusedReferencedObjects; // replace UnusedReferencedObject with any object you created in the script you load.
    }

</script>

In addition I would be glad if someone could explain me how to properly remove the last js script tag which I added with appendScriptTag(). I think I end up with multiple js included, which is actually bad.

Not sure whether it is correct as I did it there, I followed the explanation from Hendra Uzia from this post.

Community
  • 1
  • 1
Thomas Kremmel
  • 14,575
  • 26
  • 108
  • 177
  • You can remove script tags, but that doesn't remove the scripts that were parsed from those script tags. There is no way to remove scripts once they've been parsed. You will probably have to solve your problem by not loading and running the same code over and over again. – jfriend00 Aug 14 '12 at 13:41
  • 1
    As a note, this: `($(this)[0].href)` is a convoluted way of doing `this.href`. `$(this)` gives you a jQuery object containing a single element (`this`) then `[0]` returns the first element in the object (still `this`). – Anthony Grist Aug 14 '12 at 13:45
  • @AnthonyGrist thanks, will change it. jfriend00 - see my comment regarding the answer from willem Mulder – Thomas Kremmel Aug 14 '12 at 13:49

1 Answers1

3

Once a script is parsed and loaded, removing a script tag will not remove the script. So that's why those lines have no effect.

If you call modalConnect() multiple times throughout your code, the event listener on the button will also get duplicated, so that one click will cause 5 event listeners to be fired, resulting in 5 messages. That might be it...

Willem Mulder
  • 12,974
  • 3
  • 37
  • 62
  • Ok thanks. I understood that I cannot remove the added script. Still I do not understand why the console.log("function executed"); is executed multiple times. – Thomas Kremmel Aug 14 '12 at 13:47
  • .. if i cannot remove the added script, is there a way that I can check whether it already exists? maybe adding an id to the script and querying for that id? – Thomas Kremmel Aug 14 '12 at 13:51
  • 1
    Maybe because the event listener is set multiple times? For example by calling modelConnect() multiple times throughout your code? – Willem Mulder Aug 14 '12 at 13:52
  • ooh.. yes the event listener is set multiple times. I was setting it always after $( document ).ajaxStop( function() { modalConnect(); }); .. Thank you very much for that. Hmm... Now I have to think how to do the binding / unbinding correct (do you have an idea for that?) and in addition how this js loading could work correctly. – Thomas Kremmel Aug 14 '12 at 14:02
  • Solved the multiple binding issue: Since I have to recall the modalConnect() function after each ajax call I left the modalConnect() in the ajaxStop() function. But in order to not end up with multiple bindings, I added $(".editItem").unbind('click'); as first line in the modalConnect() function. This solved the multiple binding problem. Hope this is a proper approach. – Thomas Kremmel Aug 14 '12 at 14:09
  • Anyway. Thanks a million. The multiple bindings problem is solved and I will go for the approach not to add additional js tags to the dom, but to update one already available js function with a variable received via the get ajax call. Probably the more sane approach. – Thomas Kremmel Aug 14 '12 at 14:22
  • Didn't read this while I was sleeping, but sounds like you solved it! :) And yes, the unbind() and updating your script tags sound like a workable approach! – Willem Mulder Aug 15 '12 at 07:43