Set - 6

Question 1 :

How to create a popup warning box

Answer :

alert('Warning: Please enter an integer between 0 and 100.');


Question 2 :

How to create a confirmation box?

Answer :

confirm("Do you really want to launch the missile?");


Question 3 :

How to create an input box?

Answer :

prompt("What is your temperature?");


Question 4 :

How to setting a cookie with the contents of a textbox ?

Answer :

Values stored in cookies may not have semicolons, commas, or spaces. You should use the handy "escape()" function to encode the values, and "unescape()" to retrieve them.

//Sets cookie of current value for myTextBox
function TextBoxOnchange() {
	var myBox = window.document.getElementById(myTextBox");
	document.cookie = "myTextBox="+ escape(myBox.value) + getExpirationString();
}
//return a string like ";expires=Thu, 5 Jan 2006 16:07:52 UTC"
function getExpirationString() {
	var exp = new Date();
	var threemonths = exp.getTime()+(120*24*60*60*1000);
	exp.setTime(threemonths);
	return ";expires="+exp.toGMTString();
}

This is called from the event handler in the HTML.

<input name="myTextBox" type="text" id="myTextBox" onchange="javascript:TextBoxOnchange()" />


Question 5 :

How to getting values from cookies to set widgets? 

Answer :

function getCookieData(labelName) {
	//from Danny Goodman
	var labelLen = labelName.length;
	// read cookie property only once for speed
	var cookieData = document.cookie;
	var cLen = cookieData.length;
	var i = 0;
	var cEnd;
	while (i < cLen) {
		var j = i + labelLen;
		if (cookieData.substring(i,j) == labelName) {
			cEnd = cookieData.indexOf(";",j);
			if (cEnd == -1) {
				cEnd = cookieData.length;
			}
			return unescape(cookieData.substring(j+1, cEnd));
		}
		i++;
	}
	return "";
}
//init() is called from the body tag onload function.
function init() {
	setValueFromCookie("brand");
	setValueFromCookie("market");
	setValueFromCookie("measure");
}

function setValueFromCookie(widget) {
	if( getCookieData(widget) != "") {
		document.getElementById(widget).value = getCookieData(widget);
	}
}
//if you name your cookies the widget ID, you can use the following helper function
function setCookie(widget) {
	document.cookie = widget + "=" +
	escape(document.getElementById(widget).value) + getExpirationString();
}