Tricky Tricks ›› Programming Tricks ›› Javascript Tricks ›› Make A Window Open Up Automatically The First Time A Visitor Stops By A Page

Make A Window Open Up Automatically The First Time A Visitor Stops By A Page

Javascript Tricks

How can I make a window open up automatically the first time a visitor stops by a page? Can I reset it so that repeat visitors will see the window if I update the information in the window?

Here is a small JavaScript that will allow you to greet first time visitors to your site with a pop-up window. If you ever need to update the information contained in popwin.html, simply rename the cookie. As an example, in the code below, it is called COOKIE1. To make it so that nobody has ever been to your site before, change both occurances of COOKIE1 to COOKIE2 and so on...

Source Code

 Be certain when copying the source code to keep the very 
 long line below intact. You will get an error if you split
 the line. Have some fun, play with the various options such
 as height, width, and scrollbars!
 
 <SCRIPT LANGUAGE="JavaScript">
 <!--
 function GetCookie(name) {
   var arg=name+"=";
   var alen=arg.length;
   var clen=document.cookie.length;
   var i=0;
   while (i<clen) {
     var j=i+alen;
     if (document.cookie.substring(i,j)==arg)
       return "here";
     i=document.cookie.indexOf(" ",i)+1;
     if (i==0) break;
   }
   return null;
 }
 var visit=GetCookie("COOKIE1");
 if (visit==null){
    var expire=new Date();
     window.name = "thiswin";
     newwin=open("popwin.html", "dispwin",  
     "width=250,height=300,scrollbars=no,menubar=no");
    document.cookie="COOKIE1=here; expires=Thu 01-01-2004 00:00:00 GMT;";
 }
 // -->
 </SCRIPT>
 

Partners