Javascript execution order

For a web application I am using the address book function taken from one of the standard templates. Once people have been selected and the OK button clicked it populates a names field on the underlying document. This in turn fires off a function that looks up their personal details in another database and populates other fields on the document with the details.

The lookups may take between 5 - 30 seconds to do depending on the users connection to the server so I am displaying a message on the document to inform them that the look up is in progress.

JSheader

var addresswindow; //handle on address book window

Address book button

var pathname = (window.location.pathname);

document.forms[0].NamesRequireAccess.focus();

addresswindow = window.open(pathname.substring(0,(pathname.lastIndexOf(‘.nsf’)+5))+‘wAddress?OpenForm’

,‘Address’,‘status=yes,resizable=yes,scrollbars=yes,screenX=0,screenY=0,width=630,height=480’);

The last line of code in the OK button on the address book form sets the focus to another field on the underlying document to ensure that the field NamesRequireAccess onblur function runs.

NamesRequireAccess onblur event

addresswindow.close();

var targetDiv = document.getElementById(‘updateUser’);

targetDiv.innerHTML = “Looking up details from Staff Portal. Please wait”;

//alert(“blurring”);

HasAttendedSeminar();

GetPortalDetails();

The problem is that the functions HasAttendedSeminar(); and

GetPortalDetails(); are running before the address window is closed and the message displayed to the user. However if I run it with the alert box then it works in the correct order but defeats the object of what I am trying to do.

Any ideas appreciated

Subject: javascript execution order

Hi

Have you tried looking at the closed property of the dialog window? Something like…

while(addresswindow.closed==false) {

//Do nothing

}

Should make it wait until the window is actually closed before continuing.

Pete

Subject: RE: javascript execution order

I have just tried this and it still doesn’t work. I have tried an alert box immediately after the close and it returns true so never enters the while loop.

What runs in order depends on where I put an alert box. If I put one after close() the window closes but the first innerHTML never displays. If I put one after the first innerHTML statement then the window closes and the first innerHTML displays. If I don’t put any alerts in then the window closes after all this code has run and only the second innerHTML statement displays on screen.

addresswindow.close();

while(addresswindow.closed==false) {

alert(addresswindow.closed);

//Do nothing and wait for window to close

}

var targetDiv = document.getElementById(‘updateUser’);

targetDiv.innerHTML = “Looking up details from Staff Portal. Please wait”;

HasAttendedSeminar();

GetPortalDetails();

targetDiv.innerHTML = “DONE”;

}

Subject: RE: javascript execution order

At first i thik it is better to use the .innerText-property for pushing plain text in a div.

For the other problem try to use a window.timeout.

Example:

var flag = false;

function HasAttendedSeminar(){

//your code

flag = true;

}

function executeFunctions(){

HasAttendedSeminar();

while(flag == false){

   window.timeout(500, "doNothing()");

}

div.innerText = “Status”;

flag = false;

//next function an looping

}

function doNothing(){

}

In this way you can use various flags back to back.

Hope, its understandable and helps to solve the problem.

Subject: RE: javascript execution order

Thanks for pointing me in the right direction as I have now got this working. Calling window.timeout with an empty function did not work but a variation did.

JSHeader

var targetDiv;

var addresswindow;

function wait() {

setTimeout(‘executeFunctions()’, 1000);

}

function executeFunctions() {

HasAttendedSeminar();

GetPortalDetails();

targetDiv.innerText = “DONE”;

}

onLoad

targetDiv = document.getElementById(‘updateUser’);

onBlur event

addresswindow.close();

targetDiv.innerText = “Looking up details from Staff Portal. Please wait”;

wait();

Subject: RE: javascript execution order

Is it feasible to write to the main page via Javascript in the pop-up window using the window.opener object before you close the pop-up? Maybe behind an OK/Submit button or something.

Pete