Vamshi P
Vamshi P

Reputation: 23

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.

Upvotes: 0

Views: 651

Answers (2)

A. Wolff
A. Wolff

Reputation: 74420

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

Upvotes: 1

Roxoradev
Roxoradev

Reputation: 893

It's a simple solution but this will not work if your divs contains something (even if it's not jsp)

if($("#documentProperties"+id).html() == "")
     $("#documentProperties"+id).load("doctypes/"+selval+".jsp");
    else alert("Error");

You could use $.get and check if the content of the div is not equal to the result

$.get( "doctypes/"+selval+".jsp", function( data ) {
 if($("#documentProperties"+id).html() != data)
     $("#documentProperties"+id).html(data);
    else alert("Error");
});

Upvotes: 0

Related Questions