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
You can use
one()like this:.one()executes the event handler exactly once, theDate()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:$.ajax()requests ofdataType = "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):Or make the full
$.ajax()call version that$.getScript()is short for: