-1

short and sweet, I'm trying to apply the variable "eld" outside of the script so that I can apply it to the whole page or on another page via include anyone know how I can do it? (document.write (exp1, exp2, exp3, ...))

$(window).load(function(){
var mapname = "de_dolls";
var eld;

$.get("backgrounds/" + mapname + "/1.jpg")
    .done(function () {
        eld = mapname;
    }).fail(function () {
        eld = "default";    
});
document.write ( eld );
Idris
  • 997
  • 2
  • 10
  • 27

1 Answers1

0

$.get is asynchronous. The function won't have finished when you try to display it. Also document.write is not considered good coding practice. If you do this, using a callback, you'll get a better result:

<div id="thisdiv"></div>

function getBackgrounds(mapname, callback) {
  $.get("backgrounds/" + mapname + "/1.jpg")
   .done(function () {
     callback(mapname);
  })
   .fail(function () {
     callback('default');
  });
}

getBackgrounds('de_dolls', function (response) {
  $('#thisdiv').html(response);
});
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
Andy
  • 61,948
  • 13
  • 68
  • 95