Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  82] [ 4]  / answers: 1 / hits: 17421  / 12 Years ago, wed, november 14, 2012, 12:00:00

It seems that helloworld.js gets loaded multiple times based on the number of times I click #load. I say this because when I look at Google Chromes Developer Tools Network tab, it shows helloworld.js as many times as I click #load.



$(document).ready(function() {

$(#load).click(function(){
$.getScript('helloworld.js', function() {
hello();
});
});

});


The hello() function looks like this:



function hello(){
alert(hello);
}


Is it possible to detect if helloworld.js has already loaded?



So if it hasn't loaded, load it, and if it has loaded, don't load it.



This is what Developer Tools currently shows me if I click the #load button 4 times:



enter


More From » jquery

 Answers
2

Another option is letting .getScript() run but let it take the script from browser's cache so you won't have it reloaded each and every time.



To achieve this, add such code:



$.ajaxSetup({
cache: true
});


This is taken from the documentation page.


[#81994] Tuesday, November 13, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
travion

Total Points: 137
Total Questions: 96
Total Answers: 103

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
travion questions
Mon, Dec 16, 19, 00:00, 5 Years ago
Sat, Oct 19, 19, 00:00, 5 Years ago
Fri, Sep 20, 19, 00:00, 5 Years ago
Wed, Nov 14, 18, 00:00, 6 Years ago
Sun, Oct 28, 18, 00:00, 6 Years ago
;