0

here's the part of my code that is giving me the problem:

    var sellwinhtml; // global variable

    function test(){
       var testurl="https://rawgithub.com/kwagjj/mrmine-macro/master/initializing/sell_window_ver2.html";

       $.get(testurl,function(data,status){
                sellwinhtml=data;
                alert(sellwinhtml);
                }); //phase1

       var win=window.open("","name","width=300,height=300");
       win.document.write(sellwinhtml); //phase2

    }

theoretically, the code should

1) pop up and give me an alert with showing the contents of variable 'sellwinhtml'

2) open a blank window and then fill it with the html code contained in 'sellwinhtml' thereby showing a new window with something on it, eventually.

but if I run this thing the process happens the other way around.

1) a new window pops up with "undefined" written on it

2) then about a second later, the alert pop up appears showing the correct contents of 'sellwinhtml'.

This is why I thought that the problem is that the 'phase2' part was executed before 'phase1' part was finished.

Is there a way to command 'phase2' to execute when 'phase1' is absolutely finished??

kwagjj
  • 807
  • 1
  • 13
  • 23
  • The "A" in "AJAX" stands for "Asynchronous." Anything that needs to happen in response to the AJAX call should happen in the callback function, which is invoked when the call is completed. You already have a callback function defined in your call to `$.get()`, use that. – David Jan 01 '14 at 18:48
  • @David well I tried to use the callback. my needs required two subsequent callbacks so I tried to callback a callback... which sort of resulted a messy code that even I can't remember which parenthesis is closing which... – kwagjj Jan 01 '14 at 19:00
  • 1
    @kwagjj if nested callbacks are causing you headaches, look at using "deferred objects" instead, which would allow `$.get(...).done(fn1).done(fn2)...` – Alnitak Jan 01 '14 at 19:03

3 Answers3

1

You will need to add phase 2 right after phase 1 , within the callback function

function test()
{
    var testurl="https://rawgithub.com/kwagjj/mrmine-macro/master/initializing/sell_window_ver2.html";
    $.get(testurl,function(data,status)
    {
        // Phase 1
        var sellwinhtml=data;
        alert(sellwinhtml);

        // Phase 2
        var win=window.open("","name","width=300,height=300");
        win.document.write(sellwinhtml); 
    }); 
}

most important thing... add var before sellwinhtml variable..

var sellwinhtml=data; 

or else it will become a global variable and mess up all the JS code

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Piyush Ashtt
  • 340
  • 1
  • 5
  • most important thing... add var befor sellwinhtml variable var sellwinhtml=data; or else it will become globl and mess up all the JS code – Piyush Ashtt Jan 01 '14 at 18:54
  • this works too! thanks, I should be more careful where I put the codes now.. – kwagjj Jan 01 '14 at 18:58
0

Well (I hope I understood you correct) - move your popup code in the ajax callback, something like:

$.get(testurl, function(data,status){
   var win=window.open("","name","width=300,height=300");
   win.document.write(data);
});
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
0

Here you go. Structured it a bit, to clear your doubts:

var sellwinhtml; // global variable

    function test(){
       var testurl="https://rawgithub.com/kwagjj/mrmine-macro/master/initializing/sell_window_ver2.html";

       $.get(testurl,phase1); 

       function phase1(data,status) {
                sellwinhtml=data;
                alert(sellwinhtml);
                phase2();
       }

       function phase2() {
          var win=window.open("","name","width=300,height=300");
          win.document.write(sellwinhtml); //phase2
       }
    }
loxxy
  • 12,990
  • 2
  • 25
  • 56