SmootQ
SmootQ

Reputation: 2122

Unload loaded Javascript

I have 2 javascript files included in a popup (simple div) generated using ajax.

<script type="text/javascript" src="<?php echo JS ?>pm.js"></script>
<script type="text/javascript" src="<?php echo JS ?>chat.js"></script>

When you close the popup and re-open it, the jQuery bound functions in the two files execute twice.

for example

//connection
    $(document).bind('connect', function() {
            var conn = new Strophe.Connection('my-http-binding-url');
             conn.connect(inbox.data.jid, inbox.data.pass, function(status){
            inbox.connection = conn;
             alert(status)
            });
});

First time I open the popup, it displays the alert message : 5, which means it connected successfully.

But when I close the popup, and re-open it, it shows me the message twice... I assume the code is executed twice because it's not unloaded.

Upvotes: 0

Views: 221

Answers (1)

HILARUDEEN S ALLAUDEEN
HILARUDEEN S ALLAUDEEN

Reputation: 1752

You can achieve by following two different way

  1. Open your popup within a Iframe and destroy Iframe on closing the popup
  2. If you are not using Iframe, then move those script tag from "popup" template and keep them within "head" or just above closing body tag.

Note: You can not remove javascript once it loaded. Use "Firebug" or "Chrome Developer Tool" 's Network tab and make sure that javascript is not loaded multiple time.

Edit: You can access the dynamic HTML after ajax completed

$.ajax({
   ...,
   ...,
   success: function afterAjax(){
      // Insert dynamic HTML in Document
      // Any jQuery selector/function you can use here
    }
}

Upvotes: 2

Related Questions