JavaScript : Dynamic Window Handling: E-Commerce Example
Dr Benton
The Finishing Touch
Everything seems fine? Don't forget that appearances can be deceptive! Click on the button Empty basket and try to select
a product again. Nothing will happen! Of course it won't - because now you are trying to write into windows which no longer
exist!
The solution? Check that the windows exist before modifying their content and create them if necessary with a simple call
to
ouvre_fenetres().
To include this check, just add the following lines in the
switch structure of the function
ajoute_objet.
if ((caddie.closed) && (caisse.closed))
ouvre_fenetres('white','white',200,300,200,300);
This instruction will call the method
ouvre_fenêtres if the property
.closed of the window objects
caddie and
caisse is true, i.e. if the windows are closed.
Closed! Note that the JavaScript window object has not
opened property. To overcome this, you can test that the window is not closed:
!(caisse.closed)
Which is the same difference!
So now you think you've solved all your problems? Try to close one window only and select an item. Yet again, nothing happens.
The previous conditional expression is only valid if both windows are closed. We must optimise this expression to take all
possible scenarios into account:
if ((caddie.closed) || (caisse.closed))
{
if (!(caddie.closed))
caddie.close();
if(!(caisse.closed))
caisse.close();
ouvre_fenetres('white','white',200,300,200,300);
}
The first line checks to see if at least one of the windows is closed (both could be closed). If this is the case, we force
the open one to close, and finally open them both again.
And if you'd like the items already ordered to reappear when the windows are closed then reopened, it can be done! Store the
products in an array variable and add a change in the function
ouvre_fenetres() so that it updates the child windows based on the array content. You could then make only one line appear per product type,
updating the number of selected units. It's a good idea to experiment with what you have already learned.
You know how to make windows communicate, so now it's up to you to decide what they should say to one another.