Question
I have a logical error in my code. It keeps on displaying NULL in the window and the new window is blank. please explain in
I have a logical error in my code.
It keeps on displaying NULL in the window and the new window is blank.
please explain in great detail the fix.
________________________________________________________
var nameInput = document.getElementById("name").value;
var name = nameInput.value;
function setCookie(){
var nameInput = document.getElementById("name").value;
var name = nameInput.value;
if(name === ""){
alert("Error, Please enter a name");
return false;
}
const date = new Date();
date.setTime(date.getTime() + daysToLive * 24 * 60 * 60 * 1000);
let expires = "expires=" + date.toUTCString();
document.cookie = "name=" + name + "; " + expires + "; path=/";
return true;
}
function openWindow() {
var name = getCookie("name");
var message = "Welcome Cookie, " + name + "!";
window.open("", "", "width=400,height=300");
var win = window.document;
win.write("
" + message + "
");}
function getCookie(name) {
var cookieArr = document.cookie.split(";");
for (var i = 0; i
var cookiePair = cookieArr[i].split("=");
if (name == cookiePair[0].trim()) {
return decodeURIComponent(cookiePair[1]);
}
}
return null;
}
This project will challenge you to use some of what you have learned in this lesson. Create an HTML page with a form. The form will contain a text box for the user's name and a Submit button. Create a function in the section of the document that will set a cookie to the user's name submitted via the text box. Add JavaScript code to the form tag. Use the onsubmit event handler to call the function that creates the cookie. Create a code that informs the user if the name field is left blank. If the name field has a value, pass the user's name in the function call. In both the if clause and the else clause, use a return false statement to negate the submission of the form. The setting of the cookie is all that should occur for the submit event. Add another button to the HTML form. Use an onclick event handler with this scriptable button to open a new window. Write code that will display the user's name (extracted from the cookie) in a welcome message in the document of the new window
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started