Javascript to open a new popupwindow
In this article we will see how to use javascript to open a new popupwindow in a page, how to set cookies for that popup window and how to read that cookie. Also we will see how to clear cookie and again create it.
I would here tell you about how to write a javascript for:
1. Opening a pop up window
2. Setting up cookies for the pop up window
3. Reading the cookie of the pop up window.
Lets first see a javascript to open a popup window:
var newWindow;
function OpenPopUpWindowMain(Url) {
var newWindow = window.open(Url, 'WebMart', 'menubar=0,toolbar=0,scrollbars=1,resizable=0,statusbar=0,minimizable=0,width=820,height=500,top=100,left=100');
if(!newWindow)
{
alert("A popup blocker was detected. \r\n To View this pop up request to change pop up blocker setting from browser");
}
else
{
createCookie('IsPopupOpend', 'True');
newWindow.location = Url;
newWindow.focus();
}
}
Javascript to Create cookie:
function createCookie(name, value) {
var date = new Date();
date.setTime(date.getTime()+(1*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = name+"="+value+expires+"; path=/";
}
Javascript to read that cookie:
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
Javascript to clear cookie:
function eraseCookie(name) {
createCookie(name,"",-1);
}