[Javascript] setting and getting cookies across domains and folder paths using a shared iframe document- but does not work !

DEV dev at qroute.net
Sun Jun 29 13:01:56 CDT 2003


I have the following cookie functions to set and get cookies ( see the
bottom of this email if you need to).

IN my case, I have been trying to beat the bushes for the entire weekend but
could not get what I wanted to accomplish.

I have the following domains.

http://www.domain1.com
http://www.domain2.com

they both link to the same site in IIS. In other words, you can come to the
same site using either domain. But on the navigation bar and on all the site
links throughout the site, you will wind up with domain1 as all links point
to the domain1.
Since sites are identical and get served by the same server and same
wwwroot, at any point of time, you can just switch the URL from domain1 to
domain2 and vice versa, you will still be looking at the same page.

Anyway, this is just a intro.

I wanted to set a cookie which will be valid for these domains ( an info
that can be shared will equal privilidge ). My goal is to pop a message form
my visitors what their cookie count for sitevisit reach say 10. I do not
care how they accumulated that number whether you use domain1 or domain2.

I am well aware of the fact that in the cookie business, one domain cannot
read another domains cookie information otherwise it would be security
breach. I am fine with that. For that reason, I developed a trick and I have
placed an include file that penetrates all the pages throughtout the site;
since the domain1 and domain2 are all identical as far as the server and the
website is concerned ( the only diff is domain name ), that include file
even go beyond the limits of domain1.com'a pages and reach domain2.com's
pages as well !..

Of course I did not place the cookie calls directly in that include. I knew
I needed a wrapper. I thought about iframes. With iframes those two domains
I tgouht can really make a reference to the same URL !.. To really share the
same document and domain, I placed an iframe in that include file and fully
reference the iframe source document like this;

<iframe src='http://www.domain1.com/CookieSettingDocument.htm'></iframe>

What's in the  CookieSettingDocument.htm is includes below for those who'd
like to examine.( just look for the delimiters  <CODE
CookieSettingDocument.htm> and </CODE CookieSettingDocument.htm>. you will
see it in between.

I also provided the cookie library functions as well. They are in <CODE
cookieLibrary.js> and </CODE cookieLibrary.js> delimiters below.


The code works perfectly only and only when you are on the same page. I
monitor the cookie value and the status using window.status area.
So say when I am one www.domain1.com/aPage.htm I get a 1 on the sitevisit
count. If I refresh that window, I still get 1. Perfect, exactly what I
want. If I close browser sessions and come back to that page again, value
becomes 2. Exactly !..

Here the document who sets the cookie is an iframed document, you would fly
this,logic thru on the other domain ( domain2.com) or at least on the other
folders of the same domain ( domain1.com) right ??  In other words, If I
close my browserv and go to another page within the site say
http://www.domain1.com/aFolder/anotherPage.htm, I should be continuing from
exactly where I left ( which was 2 ) as far as the value right ? Not so !..
I won't get a 3 at that time. Instead it starts from square one, as if it is
a brand new cookie ???

Of course, the situation is the same for domain2. I am puzled and exhausted
in this journey. I know there is a way otherwise banner industry would not
survive. But what am I doing wrong ?? It could be something to do with path
and domain attributes of the cookie library which is quite confusing to
understand. And it does not seem like in my case, it is doing what they
should.

I do appreciate your help in this regard, if you have any alternative route
that would do what I originally I planned to do , I won't mind at all.
Thank you



<CODE CookieSettingDocument.htm>


<script>

//global variables that gover the userProbe

var runCAPTURE_SITEVISIT = true


var SITEVISIT;
var VISITLOGGED;

var SITEVISITSTAYS = 365



if (!isBrowserOK())
{

 //none IE browser, don't deal with it
 //window.status='NO IE'

}

else

{

 //IE browser, work with it

 //alert('browser is an IE ' + 'isBrowserOK')

 if (!runCAPTURE_SITEVISIT())

 {
  //window.status='NO RUN'
 }

 else

 {

  if (!isCookieON)

  {

   //cookies are not enabled.
   //window.status='cookies not on'

  }

  else

  {


   if (runCAPTURE_SITEVISIT)

   {

    //increment SITEVISIT by one     : a SITEVISIT (notexpire cookie) is
incremented only when visitLogged is not true.
    //set  VISITLOGGED to true        : visitlogged (session expire cookie )
is set to true after each first visit so that the consecutive surfings in
that session or refreshing the window does not increment SITEVISIT count


    //SITEVISIT CAPTURE CODE BEGIN

    //alert('CAPTURING SITEVISIT')

    //first make sure there is no null value in the SITEVISIT cookie

    SITEVISIT = getCookie("SITEVISIT")

    //alert('SITEVISIT obtained as ' + SITEVISIT)

    if (SITEVISIT == null)

    {

     SITEVISIT=0

     setCookie('SITEVISIT',SITEVISIT,SITEVISITSTAYS,'/');
    }


    VISITLOGGED = getCookie("VISITLOGGED")

    //alert('VISITLOGGED obtained as ' + VISITLOGGED)

    if (VISITLOGGED=='TOOKPLACE')
    {
     //alert('visitlogged is already set to to true ( ' +
getCookie("VISITLOGGED") + '), so we will not increment to the SITEVISIT
count - its already done.')
    }
    else
    {


     SITEVISIT = getCookie("SITEVISIT") * 1

     SITEVISIT = SITEVISIT + 1

     setCookie('SITEVISIT',SITEVISIT,SITEVISITSTAYS,'/');

     setCookie('VISITLOGGED','TOOKPLACE',null,'/');

     //alert('visitlogged is not set to to true yet ( ' +
getCookie("VISITLOGGED") + ') , so we can increment the SITEVISIT count')

    }



    //============================================================
    window.status = 'SITEVISIT='+ SITEVISIT
    //============================================================



   }

   else

   {

    //alert('runCAPTURE_SITEVISIT is set to false')

   }


  }

 }

}




</script>


</CODE CookieSettingDocument.htm>
























here is my library functions.

<CODE cookieLibrary.js>


//

function isCookieON()
{
 var cookieBackup = document.cookie
 document.cookie = "cookie=yep"
 var cookieOk = document.cookie.indexOf("cookie=yep") > -1
 document.cookie = cookieBackup
 if (cookieOk) {
  return true;
 }
 else
 {
  return false;
 }
}



function setCookie(Name,Value,Expiry,Path,Domain,Secure) {
   //Bunch of arguments

if (Expiry != null) {
   //if you want to save the cookie

var datenow = new Date();
   //get a date

datenow.setTime(datenow.getTime() + Math.round(86400000*Expiry));
   //mutiply the number to make it represent days

Expiry = datenow.toGMTString();
   //convert to GMT time

}
   //ends that. And now...

Expiry = (Expiry != null) ? '; expires='+Expiry : '';
   //has an expiration?

Path = (Path != null)?'; path='+Path:'';
   //has a path?

Domain = (Domain != null) ? '; domain='+Domain : '';
   //has a domain?

Secure = (Secure != null) ? '; secure' : '';
   //Secure?

document.cookie = Name + '=' + escape(Value) + Expiry + Path + Domain +
Secure;
   //Make the cookie!

}

function getCookie(Name) {
   //Your name goes here! :-)

var cookies = document.cookie;
   //Copy your cookies

if (cookies.indexOf(Name + '=') == -1) return null;
   //Woops, no cookie!

var start = cookies.indexOf(Name + '=') + (Name.length + 1);
   //Jump to start of cookie

var finish = cookies.substring(start,cookies.length);
   //Get a count from the cookies

finish = (finish.indexOf(';') == -1) ? cookies.length : start +
finish.indexOf(';');
   //Find end of cookie

return unescape(cookies.substring(start,finish));
   //Here's your cookie! ( Sorry, no chocolate chips. :-)

}

/*
 Syntax rules:

***********************    Making the Cookie / Reading the Cookie
***********************

   A. Name
       1. No characters besides A-Z, a-z, numerals, and the underscore.
       2. Probably don't want to use an especially long name.
   B. Value
       1. Here's the actual content.
       2. Anything goes here up to 4000 characters.
   C. Expiry (This is what shines!)
       1. Type in the NUMBER OF DAYS to save the cookie or:
       2. Leave this out and the cookie will expire during the current
session.
   D. Path
       1. Sets a restriction on the portion of the server allowed to access
a cookie.
       2. Use a slash "/" to allow any page from your site to obtain the
cookie.
   E. Domain
       1. Sets a domain as in MAIL.yahoo.com  ( EMPHASIS ADDED!!! ).
       2. By default this would be set to the same as the cookie's origin
domain.
   F. Secure
       1. If you want access to the cookie ONLY during a secure connection.
       2. Normally you'd want this to protect sensitive information.

   You would type this...

setCookie('friendly_cookie','Mr. Smith is tall.',18,'/');

   ...to make a cookie with a NAME of friendly_cookie, a VALUE of "Mr. Smith
is tall.", an
   EXPIRATION of 18 days later, and having the most accessible PATH of /.


   You would type this...

var YOUR_VARIABLE_NAME = getCookie('friendly_cookie');

   ...to set the variable of YOUR_VARIABLE_NAME to the value of the cookie
with a NAME of
   friendly_cookie or to null if there was no cookie available named
friendly_cookie.


*/

</CODE cookieLibrary.js>



More information about the Javascript mailing list