tags:

views:

78

answers:

1

I am trying to load a file.js dynamically with $.getScript. I have several select options to load different script on each click. I just want to call it once and if possible remove it from the dom on clicking another option, but firebug says loading the same script again and again on the next subsequent clicks. Having tried several solutions, but found unexpected results:

  1. placing unbind click, or event right in the callback. This one kills the click event on the next click. I still want the click to happen. Perhaps I did it wrong.
  2. using .length to check if the url is there to no luck
  3. using variables/ function in the script if it is there in the dom already

Did I miss anything, or is it supposed to be so? I found an eternal question on this matter in jquery forum alone :) Hope found better luck here. Thanks for kind help.

+6  A: 

This is the equivalent of getScript behind the scenes. Setting the cache option to true should resolve your issue.

$.ajax({
  url: url,
  dataType: 'script',
  success: success,
  cache: true
});
ChaosPandion
My, it did come across my mind in less than a millisecond when looking around for solution :) Only never thought that it has dataType: 'script', dan cache: true can save my life. I'll report back when I get it right. Thanks a million
swan
@kuswantin: No problem, I would rather they have some kind of dataType enum for that option. That way if your using a nice IDE you can get intellisense. Anyway, just remember to keep a tab open with the jQuery documentation, you never need to memorize anything. As you use jQuery you'll know what to look up even though you can't remember the specifics.
ChaosPandion
This way I can even replace my old ajax php page with a simple call to jquery file. Thanks for the precious gems.
swan