views:

52

answers:

2

Hi Guys,

I am trying to load 2 scripts in jquery only once a #element is clicked. Currently, I am trying to use:

    jQuery("#element").click(function () {
       jQuery('#elementcontainer').load('/js/jquery.script.js');
       jQuery('#elementcontainer').load('/js/jquery.script2.js');
    });

The problem is that the scripts load everytime the #element is clicked. I only want the scripts to load once - and currently they also make like this

"http://127.0.0.1/pck/Js/jquery.script.js?_=1279101767931"

How can I load the scripts ONLY once and stop the non-cache. I am using ASP.NET MVC ?

Thx

A: 

This will do the trick:

  jQuery("#element").click(function () {
       jQuery('#elementcontainer').load('/js/jquery.script.js');
       jQuery('#elementcontainer').load('/js/jquery.script2.js');
       jQuery(this).unbind('click');
    });

Note the unbind() method called on $(this) (which is the element #element). You can check more unbind() functionality here.

Bogdan Constantinescu
+5  A: 

You can use one() like this:

jQuery("#element").one('click', function () {
   jQuery('#elementcontainer').load('/js/jquery.script.js?' + (new Date().getTime()));
   jQuery('#elementcontainer').load('/js/jquery.script2.js?"' + (new Date().getTime()));
});

.one() executes the event handler exactly once, the Date() piece is for the cache. I'm no sure you want .load() though, if your intent is just to get/execute the scripts, then $.getScript() is more appropriate, like this:

jQuery("#element").one('click', function () {
   jQuery.getScript('/js/jquery.script.js');
   jQuery.getScript('/js/jquery.script2.js');
});

$.ajax() requests of dataType = "script" (which $.getScript() is) won't be cached.


If you want to cache, you have two options, you can use $.ajaxSetup() once (but this affects all subsequent $.ajax() or shorthand calls):

$.ajaxSetup({ cache: false });

Or make the full $.ajax() call version that $.getScript() is short for:

jQuery("#element").one('click', function () {
   jQuery.ajax({ url: '/js/jquery.script.js', dataType: 'json', cache: true });
   jQuery.ajax({ url: '/js/jquery.script2.js', dataType: 'json', cache: true });
});
Nick Craver
ok cool thx so much ill give this a try now :) can I just replace the .load with .getscript ? also I want the scripts to cache. current they add the ?_=2312312312 but I don't want them too ?
Tom
@Tom - Yup, I added an example...if you look at your console, getscript will add the time to the querystring under the covers, it happens here: http://github.com/jquery/jquery/blob/master/src/ajax.js#L269
Nick Craver
@Tom - They need to add that...that's *how* they instruct the browser not to cache, otherwise IE in particular will hold onto them.
Nick Craver
@Nick Craver: +1 for that caching thing, noobs run into problems as they don't know requests being cached in those cases.
Sarfraz
hey thanks so much nick - if I want to cache the scripts ? can I do this with the above code ? i.e. currently the ?=2312312 is added but I would prefer they were cached ?
Tom
@Tom - I *completely* misread that, you *want* to cache, one moment.
Nick Craver
I like the idea of examples of cached and not, gives flexibility.
Mark Schultheiss
ah ok great thx. the only problem I thougt is I would have to use EncodeString() or something like this :)
Tom
@Tom - I added how to fetch them *without* that timestamp for caching as well :)
Nick Craver
legend! thx so much :)
Tom