Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- New Perspectives on JavaScript, 2nd Edition
- Tutorial 9
- Review Assignment
- Author: kathy bampfield
- Date: 11/21/2015
- Filename: profile.js
- Functions List:
- getSelection(selectionList)
- Retrieves the index number of the selected option from the selection
- list, selectionList.
- getOption(optionGroup)
- Retrieves index number of the selected radio button from the
- group of radio buttons with the name, optionGroup
- initPage()
- Adds an event handler to the profile form submit button
- when the page is opened.
- saveProfile()
- Saves the values in the profile form to fields in
- the memberInfo multi-valued cookie
- */
- function getSelection(selectionList) {
- return parseInt(selectionList.selectedIndex);
- }
- function getOption(optionGroup) {
- for (var i = 0; i < optionGroup.length; i++) {
- if (optionGroup[i].checked) return parseInt(i);
- }
- }
- /* Add new code below */
- addEvent(window, "load", initPage, false);// runs Init function when page is loaded
- function initPage() {
- if (cookiesEnabled() == false) // call cookies enabled to see if cookies are enabled by browser
- alert("You must have cookies enabled to save member profile.");
- // add event handler to the submit button
- document.getElementById("submitButton").onclick = saveProfile; // when you click button runs the saveProfileInfo()
- }
- function cookiesEnabled(){ // test to see if cookies are enabled in browser
- var cookiesTest = false;
- storeCookie("tempCookie", "temp");
- if (getCookie("tempCookie")){
- cookiesTest = "test";
- removeCookie("tempCookie");
- }
- }
- function saveProfile(fname, ) { // Saves the values in the profile form to fields in
- // the save profile multi-valued cookie
- //storeCookieField() set to expire 6 month
- var expDate = new Date(); //expire store the cookie expiration date
- expDate.setMonth(expDate.getMonth() + 6); //set to expire in 6 months
- expString = "; expires=" + expDate.toGMTString();
- var allFields = document.profileForm.elements;//loop through all elements in the form
- for (var i = 0; i < allFields.length; i++) {
- if (allFields[i].type == "text") {
- storeCookieField(allFields[i].id, allFields[i].value, expire);
- }
- if (allFields[i].nodeName == "SELECT") {
- storeCookieField(allFields[i].id, allFields[i].selectedIndex, expire);//getSelection pg 539
- }
- if (allFields[i].type == "radio" || allFields[i].type == "checkbox") {//getOption pg 539
- storeCookieField(allFields[i].id, allFields[i].split, expire);
- }
- alert("Profile Saved");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement