views:

796

answers:

1

Hi All

I have a site with five tabs using jquery-ui. Each loads an individual jQuery script. Now when I have loaded the second tab and go to the third tab the js out of the second tab still remains active and disturbs my actions in the third tab space.

Can someone explain me why the js out of the second tab still stays active when changing to the third tab and how I can avoid such behaviour?

+1  A: 

Once you have loaded a script onto the page, it remains active until the page refreshes. I don't know about unloading, but you can certainly disable the actions of a certain script depending on what it is. Could you post an example of the script causing the issues?

EDIT: For example, if you have a script that is dependent on the tab you are in, you can condition the actions in the script with the tabs being at a certain index. A demo syntax:

//will only execute action if you are in current tab

if(tabs_ui_index == 0){
  //do something
}

to get that variable (tabs_ui_index) you can do something like this:

 $('#mytabs').bind('tabsselect', function(event, ui) {
     tabs_ui_index = ui.index;
 });

This code binds a "tabsselect" event to the element that is tabbed and sets the variable to the currently indexed tab every time a user changes a tab.

Also, you can unbind events, if you loaded a script that set a click event for a button that no longer exists, or that you are using for a different purpose now:

$("#mybutton").unbind("click");

I hope this helps. Let me know if you have any other questions.

yuval
I would like to post the script but there are multiple scripts of them referencing to each other so it would make more sense to post the whole application so it is understandable what the scripts does. Do you maybe have a solution on how I can post these in an understandable way?
elhombre
in that case, i'll give you a more general response. see edited answer
yuval
Managed to solve my problem in my own way, but your explanation explains what I did wrong. (I made the failure that I loaded in every tab a js File, so when I came back to the same tab it would load again the same JS File. The output would then look duplicated because the JS runs two times in the background)
elhombre