/    Sign up×
Community /Pin to ProfileBookmark

need help explain a function

Hi guys, you were good enough to help me back in December, now I have an issue with some code given to me from the javascript course I am following – the tutors haven’t helped much?

Can anyone explain what the function “closeAllWindows” ‘actually’ does?

here’s the code…

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>

<HTML>
<HEAD>
<TITLE>CIW Products</TITLE>

<SCRIPT LANGUAGE=”JavaScript”>
<!–
var content;
var oneWindow;
var fullWindow;
var oneWindowStatus = false;
var fullWindowStatus = false;

function closeAllWindows() {
if((oneWindowStatus) && (!oneWindow.closed)) oneWindow.close();
if((fullWindowStatus) && (!fullWindow.closed)) fullWindow.close();
oneWindowStatus = false;
fullWindowStatus = false;
}

function productObject(id,item,description,price,image) {
this.id = id;
this.item = item;
this.description = description;
this.price = price;
this.image = image;
this.displayOne = displayOne;
}

//Add products here
var products = new Array();
products[0] = new productObject(“cp-01”, “CIW Backpack”, “Black woven backpack, blue-green lettering”, “$25.00”, “images/backpack.jpg”);
products[1] = new productObject(“cp-02”, “CIW Hat”, “Brown canvas hat, blue-green lettering”, “$20.00”, “images/hat.jpg”);
products[2] = new productObject(“cp-03”, “CIW Suede Jacket”, “Green suede, blue lettering”, “$110.00”, “images/suede.jpg”);
products[3] = new productObject(“cp-04”, “CIW Rain Jacket”, “Green rainproof, blue lettering”, “$100.00”, “images/rain.jpg”);
products[4] = new productObject(“cp-05”, “CIW Polo Shirt”, “White mesh polo, green lettering”, “$30.00”, “images/polo.jpg”);
products[5] = new productObject(“cp-06”, “CIW Denim Shirt”, “Dark blue denim, blue lettering”, “$50.00”, “images/denim.jpg”);
products[6] = new productObject(“cp-07”, “CIW Sweatshirt”, “Gray fleece pullover, green lettering”, “$70.00”, “images/sweat.jpg”);
products[7] = new productObject(“cp-08”, “CIW Sport Shirt”, “Blue, blue-green lettering”, “$30.00”, “images/sport.jpg”);
products[8] = new productObject(“cp-09”, “CIW Sweater”, “Blue scoop neck, short sleeve, green lettering”, “$60.00”, “images/sweater.jpg”);
products[9] = new productObject(“cp-10”, “CIW Notebook”, “Black leather, blue lettering”, “$75.00”, “images/notebook.jpg”);

var len = products.length;

function displayOne() {
content=””;
content+=”<HTML><HEAD><TITLE>” + this.item + “</TITLE></HEAD>”;
content+=”<BODY><DIV ALIGN=’center’>”;
content+=”<TABLE WIDTH=’90%’><TR><TD COLSPAN=’2′ ALIGN=’center’>”;
content+=”<H3>Item: ” + this.item.bold() + “</H3><HR>”;
content+=”<TR><TD><B>ID:</B> ” + this.id;
content+=”<TR><TD><B>Description:</B> ” + this.description;
content+=”<TR><TD><B>Price:</B> ” + this.price.bold().fontcolor(“#0000FF”);
content+=”<TR><TD><IMG SRC= ‘” + this.image + “‘ HEIGHT=’75’ WIDTH=’75’>”;
content+=”</TABLE><FORM>”;
content+=”<INPUT TYPE=’button’ VALUE=’OK’ onClick=’window.close();’>”;
content+=”</FORM></DIV></BODY></HTML>”;
oneWindow = open(“”,”OneWindow”,”width=450,height=290″);
newWindow(oneWindow);
oneWindowStatus = true;
}

function findItem() {
var i = 0;
while (i < len && products[i].id != document.prodForm.idText.value.toLowerCase()) {
i++;
}
if (i < len) {
var message = “”;
message += “ID:t” + products[i].id;
message += “nItem:t” + products[i].item;
alert(message);
} else {
var message = “It appears you have entered a discontinued id.n”;
message += “Please try again.”;
alert(message);
}
}

function showAll() {
content=””;
content+=”<HTML><HEAD><TITLE>All CIW Products</TITLE></HEAD>”;
content+=”<BODY><DIV ALIGN=center>”;
content+=”<TABLE WIDTH=’85%’ BORDER CELLPADDING=’2′>”;
content+=”<TR><TH COLSPAN=’2′ ALIGN=’center’>”;
content+=”CIW Product Information”;
content+=”<TR><TH>Item<TH>Price”;

for (var i = 0; i < len; i++) {
content+=”<TR><TD>”;
content+=”<A HREF=’javascript:void(window.opener.products[” + i + “].displayOne());’>”;
content+=products[i].item + “</A>”;
content+=”<TD>” + products[i].price;
}
content+=”</TABLE><FORM>”;
content+=”<INPUT TYPE=’button’ VALUE=’OK’ onClick=’window.close();’>”;
content+=”</FORM></DIV></BODY></HTML>”;
fullWindow = open(“”,”AllWindow”,”width=450,height=395,resizable=1″);
newWindow(fullWindow);
fullWindowStatus = true;
}

function newWindow(x) {
x.document.close();
x.document.open();
x.document.write(content);
x.document.close();
x.moveTo(20,20);
x.focus();
}

//–>
</SCRIPT>

</HEAD>
<BODY onFocus=”closeAllWindows();”>
<DIV ALIGN=”center”>

<H2>CIW Products</H2>
<HR>
<FORM NAME=”prodForm”>
<TABLE WIDTH=”100%”>
<TR>
<TD WIDTH=”50%” ALIGN=”right”>
<SELECT NAME=”itemName”>

<SCRIPT LANGUAGE=”JavaScript”>
<!–

for (var i = 0; i < len; i++) {
document.write(“<OPTION>” + products[i].item);
}

//–>
</SCRIPT>

</SELECT>
<TD>
<INPUT TYPE=”button” VALUE=”Get Info”
onClick=”
var i = document.prodForm.itemName.selectedIndex;
products[i].displayOne();”>
<TR><TD ALIGN=”right”>
<B>Search by product ID:</B> <INPUT TYPE=”text” SIZE=”5″ NAME=”idText”>
<TD>
<INPUT TYPE=”button” value=”Search” onClick=”findItem();”>
<TR><TD colspan=”2″ ALIGN=”center”>
<TR><TD colspan=”2″ ALIGN=”center”>
<INPUT TYPE=”button” VALUE=”Show All Products” onClick=”showAll();”>
</TABLE>
</FORM>
</DIV>
</BODY>
</HTML>

Thanks in advance, a very confused Dazzyd!

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledJan 24.2009 — before anyone explain you, what is your actual interpretation of that function?

i'm pretty sure some people will come and simply tell you what the function does. however, i'm the type of guys that 'we are here to help you, not to give the final solution or answer'. (very sorry but is for your own good).
Copy linkTweet thisAlerts:
@dazzydauthorJan 24.2009 — zeroKilled, i am but a lowly designer who has ventured in coding...

I thought at first it closed any open windows but having removed the Body onFocus call to it, I "think" that it could have something to do with the datastream; I have read the notes from the course over and over but they don't help much? I can only contact the tutors through email and most of their help are riddles....

I understand the constructor, the array and eventually I understood the populating of the <option> list, but this has me stumped - any clues?
Copy linkTweet thisAlerts:
@ZeroKilledJan 24.2009 — I thought at first it closed any open windows but having removed the Body onFocus call to it,[/QUOTE]
that is what it basically does. the function close two possible windows (if opened) when an onfocus event occur in the main window.
Copy linkTweet thisAlerts:
@dazzydauthorJan 24.2009 — zeroKilled, thank you?

so, do I gather that fullwindow.close is a built-in object/property ( I'm still a little confused by the correct terminology) - i have seen references to fullwindow on some JS websites.

But I have not come across any references to onewindow - would I be right saying that it is a built-in function dealing with a current open window?

But why would the script seem to work okay with the call to the function removed?
Copy linkTweet thisAlerts:
@ZeroKilledJan 24.2009 — But why would the script seem to work okay with the call to the function removed?[/QUOTE]
function are block of statements that don't execute when the script is loaded. that is, function are pieces of code with so many purpose: to call it later (moslikly for events like the onfocus, to perform repetitive task, or even to perform some algorithm with certain data (the arguments) in order to produce different result (like giving two number for been divided).

both variable [B]fullwindow[/B] and [B]onewindow[/B] is a variable identifier, that aren't built-in property. those are name given by the coder for two windows object. however, the [B]close[/B] method and the [B]closed[/B] property are indeed built-in.
Copy linkTweet thisAlerts:
@dazzydauthorJan 24.2009 — zeroKilled, i hope you dont get bored with my questions...

because now i'm a little more confused..

If I have this right...

fullWindow and oneWindow are just variables... but to what use?

There is also a reference to a AllWindow in the function showall() - is that just a variable as well? I can't see a use for it...

The function closeAllWindows is called by onFocus when the page is loaded, but the app seems to function perfectly ok when the onFocus closeAllWindows() is removed from the html code.

my question is - is this function really necessary or am I being dumb?
Copy linkTweet thisAlerts:
@dazzydauthorJan 24.2009 — is there anyone who is able to help me?
Copy linkTweet thisAlerts:
@ZeroKilledJan 25.2009 — [B]fullWindow[/B] and [B]oneWindow[/B] are just variables... but to what use?[/QUOTE]

both are supposed to be variable, as any other defined by the coder, that contain a reference to a [I]window object[/I]. [b]how i know it?[/b] first because the name of the variable and second, and more important, because the property and method it use (closed and close). [I]notice: the name of a variable is useless as long it don't conflict with reserved keywords. i.e. both variable could be named as [b]fw[/b] and [b]ow[/b], but for good practice those variable are name as so with the purpose to be self descriptive.[/I] the solely reason of those variable is to retain a reference to opened window by javascript (of course, those that you explicitly open with window.open method).


There is also a reference to a AllWindow in the function showall() - is that just a variable as well?[/QUOTE]
[B]AllWindow[/B] is not a variable, it is a string passed as one of the argument of window.open method.

window.open()

The function closeAllWindows is called by onFocus when the page is loaded[/QUOTE]
that statement is incorrect, an onfocus event trigger when some kind of element receive focus after loosing it. that kind of element can vary because onfocus event can be applied to various element. in this case, onfocus is applied to the body element which take effect when the window become active after loosing focus.

but the app seems to function perfectly ok when the onFocus closeAllWindows() is removed from the html code ... is this function really necessary ...?[/QUOTE]
in this particular case the overall application work perfectly because it isn't a mandatory requirement to close the windows. that is, the code for closing windows is an extra code that isn't part of the main functionality of the application. so, the obvious answer is that the code isn't necessary.
Copy linkTweet thisAlerts:
@dazzydauthorJan 25.2009 — zeroKilled

thank you for your time in helping me?

Most of the code I have managed to understand, so hopefully by the end of this year i'll have it all sorted out!! ?
×

Success!

Help @dazzyd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...