Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. /*
  2. New Perspectives on JavaScript, 2nd Edition
  3. Tutorial 9
  4. Review Assignment
  5.  
  6. Author: kathy bampfield
  7. Date: 11/21/2015
  8.  
  9. Filename: profile.js
  10.  
  11. Functions List:
  12.  
  13. getSelection(selectionList)
  14. Retrieves the index number of the selected option from the selection
  15. list, selectionList.
  16.  
  17. getOption(optionGroup)
  18. Retrieves index number of the selected radio button from the
  19. group of radio buttons with the name, optionGroup
  20.  
  21. initPage()
  22. Adds an event handler to the profile form submit button
  23. when the page is opened.
  24.  
  25. saveProfile()
  26. Saves the values in the profile form to fields in
  27. the memberInfo multi-valued cookie
  28.  
  29. */
  30.  
  31.  
  32. function getSelection(selectionList) {
  33. return parseInt(selectionList.selectedIndex);
  34. }
  35.  
  36. function getOption(optionGroup) {
  37. for (var i = 0; i < optionGroup.length; i++) {
  38. if (optionGroup[i].checked) return parseInt(i);
  39. }
  40. }
  41.  
  42.  
  43. /* Add new code below */
  44.  
  45. addEvent(window, "load", initPage, false);// runs Init function when page is loaded
  46.  
  47. function initPage() {
  48.  
  49. if (cookiesEnabled() == false) // call cookies enabled to see if cookies are enabled by browser
  50. alert("You must have cookies enabled to save member profile.");
  51.  
  52.  
  53. // add event handler to the submit button
  54. document.getElementById("submitButton").onclick = saveProfile; // when you click button runs the saveProfileInfo()
  55. }
  56.  
  57. function cookiesEnabled(){ // test to see if cookies are enabled in browser
  58.  
  59. var cookiesTest = false;
  60. storeCookie("tempCookie", "temp");
  61.  
  62. if (getCookie("tempCookie")){
  63. cookiesTest = "test";
  64. removeCookie("tempCookie");
  65. }
  66. }
  67.  
  68. function saveProfile(fname, ) { // Saves the values in the profile form to fields in
  69. // the save profile multi-valued cookie
  70. //storeCookieField() set to expire 6 month
  71.  
  72. var expDate = new Date(); //expire store the cookie expiration date
  73.  
  74. expDate.setMonth(expDate.getMonth() + 6); //set to expire in 6 months
  75. expString = "; expires=" + expDate.toGMTString();
  76.  
  77. var allFields = document.profileForm.elements;//loop through all elements in the form
  78. for (var i = 0; i < allFields.length; i++) {
  79.  
  80. if (allFields[i].type == "text") {
  81. storeCookieField(allFields[i].id, allFields[i].value, expire);
  82. }
  83. if (allFields[i].nodeName == "SELECT") {
  84. storeCookieField(allFields[i].id, allFields[i].selectedIndex, expire);//getSelection pg 539
  85. }
  86. if (allFields[i].type == "radio" || allFields[i].type == "checkbox") {//getOption pg 539
  87. storeCookieField(allFields[i].id, allFields[i].split, expire);
  88. }
  89. alert("Profile Saved");
  90.  
  91. }
  92.  
  93.  
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement