Download jQuery Script Once onClick

I try to load 2 scripts in jquery only after clicking on the #element element. I am currently 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 are loaded every time the #element button is pressed. I only want the scripts to load once - and currently they also do this as

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

How can I load scripts ONLY once and stop no cache. Am I using ASP.NET MVC?

thank

+3
source share
2 answers

You can use one()as follows:

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() , Date() . , .load(), , / , $.getScript() , :

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

$.ajax() dataType = "script" ( $.getScript() is) .


, , $.ajaxSetup() ( $.ajax() shorthand)

$.ajaxSetup({ cache: false });

$.ajax(), $.getScript() :

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 });
});
+9

:

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

unbind(), $(this) ( #element). unbind() .

0

Source: https://habr.com/ru/post/1754595/


All Articles