Vamshi P Vamshi P - 5 months ago 13
Javascript Question

jquery load() only once for one file

$("#documentProperties"+id).load("doctypes/"+selval+".jsp");


here selval is value of a dropdown option. dropdown options and the jsp's which i am loading
have the same names. that way i am using jquery's load() to load the jsp file into a div.

here problem is there are multiple div's which generate dynamically with name documentProperties and
a number attatched to it for id uniqueness. i need to load the jsp only once, if one jsp is already loaded
into a div don't load that, show any alert error saying this document is already loaded.

Answer

What about adding a class:

$("#documentProperties"+id + ":not(.loaded)").load("doctypes/"+selval+".jsp", function(){
  $(this).addClass('loaded');
});

EDIT:

var jspLoaded = [];

Later:

if (jspLoaded.indexOf(selval) === -1) {
  $("#documentProperties" + id).load("doctypes/" + selval + ".jsp", function() {
    jspLoaded.push(selval);
  });
}