In this chapter, we looked at how we could put frames into our web pages and how to write JavaScript code that scripted between frames. We also looked at how to open and modify new browser windows using script.
In the previous chapter's exercise questions, we created a form that allowed the user to pick a computer system. He could view the details of his system and its total cost by clicking a button that wrote the details to a textarea. Change the example so that it's a frames-based web page, and instead of writing to a text area, the details should be written to another frame. |
||
A: | The solution I have created involves a frameset that divides the page into two frames, left and right. In the left we have the form that allows the user to pick his system. The system chosen is summarized in the right frame when the user clicks an Update button. The first page is the top frameset-defining page and is the one that needs to be loaded into the browser first. <html> <head> </head> <frameset cols="55%,*"> <frame src="PickSystem.htm" name="pickSystem"> <frame src="blank.htm" name="systemSummary"> </frameset> </html> Save this as ch7Q1TopFrame.htm. Next we need to create a blank page for the right frame. Until the user clicks an Update button, that frame is blank, but if we don't put an HTML page into that frame, Netscape throws an error, though IE is happy with src="". <html> <body> </body> </html> Save this as blank.htm. Finally, we come to the page that's loaded into the left frame and allows the user to choose his computer system and its components. This is very similar to the solution to Question 2 in the previous chapter, so I'll just concentrate on what's been changed here. All the changes are within the updateOrderDetails() function. function updateOrderDetails() { var total = 0; var orderDetails = "<H3>Your selected system</H3>"; var formElement; formElement = document.form1.cboProcessor[document.form1.cboProcessor.selectedIndex]; total = parseFloat(CompItems[formElement.value]); orderDetails = orderDetails + "Processor : " + formElement.text orderDetails = orderDetails + " $" + CompItems[formElement.value] + "<br>"; formElement = document.form1.cboHardDrive[document.form1.cboHardDrive.selectedIndex]; total = total + parseFloat(CompItems[formElement.value]); orderDetails = orderDetails + "Hard Drive : " + formElement.text orderDetails = orderDetails + " $" + CompItems[formElement.value] + "<br>"; formElement = document.form1.chkCDROM if (formElement.checked == true) { orderDetails = orderDetails + "CD-ROM : $" + CompItems[formElement.value] + "<br>"; total = total + parseFloat(CompItems[formElement.value]); } formElement = document.form1.chkDVD if (formElement.checked == true) { orderDetails = orderDetails + "DVD-ROM : $" + CompItems[formElement.value] + "<br>"; total = total + parseFloat(CompItems[formElement.value]); } formElement = document.form1.chkScanner if (formElement.checked == true) { orderDetails = orderDetails + "Scanner : $" + CompItems[formElement.value] + "<br>"; total = total + parseFloat(CompItems[formElement.value]); } formElement = document.form1.radCase if (formElement[0].checked == true) { orderDetails = orderDetails + "Desktop Case : $" + CompItems[formElement[0].value] + "<br>"; total = total + parseFloat(CompItems[formElement[0].value]); } else if (formElement[1].checked == true) { orderDetails = orderDetails + "Mini Tower Case : $" + CompItems[formElement[1].value] + "<br>"; total = total + parseFloat(CompItems[formElement[1].value]); } else { orderDetails = orderDetails + "Full Tower Case : $" + CompItems[formElement[2].value] total = total + parseFloat(CompItems[formElement[2].value]); } orderDetails = orderDetails + "<P>Total Order Cost is $" + total + "</P>"; window.parent.systemSummary.document.open(); window.parent.systemSummary.document.write(orderDetails); window.parent.systemSummary.document.close(); } Save this as PickSystem.htm, and load ch7Q1TopFrame.htm into your browser to try out the code. The first difference between this and the code from Question 2 in the last chapter is that when creating the text summarizing the system we are creating HTML rather than plain text, so rather than \n for new lines we use the <br> tag. The main change, however, is the following three lines: window.parent.systemSummary.document.open(); window.parent.systemSummary.document.write(orderDetails); window.parent.systemSummary.close(); Instead of setting the value of a textarea box as we did in the solution to Question 2 in the last chapter, this time we are writing the order summary to an HTML page, the page contained in the right-hand frame, systemSummary. First we open the document for writing, then write out our string, and finally close the document indicating that we have completed our writing to the page. |