Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie Question... 1

Status
Not open for further replies.

coldfused

Technical User
Jan 27, 2001
2,442
US
Can I do this?

Home page set a cookie and give it the variable of:

"page=1"

Rest of pages check for variable "page=1"

if page = 1 the page now = 2
elseif page = 2 page now = 3

if page = 3 popup window open

What i want to do is trac users to a third page view, once on the third page view i would like to have a teaser ad pop up enticing them to sign up for special giveaway.

Now i know all about pop ups and cookie browser issues so i dont really need to hear all the details on why i should not do this.

My boss insist that we do this because he heard it somewhere and wants to get it done, so i am forced to find a solution.

Anyone have any ideas on how to impliment this, i'm not that familiar with cookies so an example would be great if someone new how and had the time.

Thanks in advance,

---------------------------------------------
 
I definitely got and issue looking at your URL on my Mac.

I will try to troubleshoot it to see what it might be.

Wow JT that almost looked like you knew what you were doing!
 
I am afraid I haven't had a chance to dig into it. I'm dealing with major PC related issues in the home environment. I will attempt to look at it tonite.

Sorry I didn't mean to leave you hanging.

For your expiration it's:

Code:
document.cookie = "expires=" //enter date or + date variable

Hope that helps.

Wow JT that almost looked like you knew what you were doing!
 
Hi pixl8r,

I'm finally gettig back to this as a bunch of stuff got pushed ahead of it.

Nonetheless I figured out why it wasn't working on a mac, when I copied and pasted the code from here onto my mac a bunch of extras charecters got added, once removed all is well.

Now my issue is on a pc once the pop up comes on the third page, thats it, it doesnt show again unless they clear there cookie, All good.

On a mac however once they get to the third page and it launches, every page aftr that it also launches i'm guessing cause the value of the cookie is being left at 3 and the mac relaunches every page after.

How can I set the page to where on a mac it doesnt re-launch? Or once the cookie value=3 launch the pop up but reset the value back to 0 that way atleast it wont launch every single time?

Thanks,

----------------------------------------
 
I'm glad you figured out the Mac thing. I was at a loss. I couldn't duplicate the problem on my development evironment but could get it on yours.

On the other try changing the if statement in the incrementPageNumber() function.

Code:
function incrementPageNumber(i){
   if (i <= 2) { //if the page number is not equal to 2
      i++;  //add one to the page number
      document.cookie="pageNumber="+i;  //and write it to the cookie
      alert(getCookie("pageNumber")); //for testing remove when live
   }
   else{
   //open the pop up window
      if (getCookie("windowOpened") != "true"){ //if the window hasn't already been opened
        window.open('popup.htm','advertisement','width=200,height=200');
        document.cookie="windowOpened = true";
        }
   }
}

Otherwise the "windowOpened" parameter in the cookie should be preventing the window from opening. Let me know if the change above does the trick.

Wow JT that almost looked like you knew what you were doing!
 
Here JT have a look now. I changed the function as you stated above, actually stopped working. So I put it back the way it was and tested again:

As you can see once it launches, once on the third page, it will launch every page afterwards, mac or pc:


the script is here:


Thanks,



----------------------------------------
 
Yep it sure does do that.

I made a slight alteration to the script.

Code:
function setPageNumber(){
   //Insert current page number
   var currentPageCount = getCookie("pageNumber");
   if (getCookie("pageNumber")==null){//if there is no cookie
      document.cookie="pageNumber=1";//create the cookie
      document.cookie="windowOpened=false";//this is NEW
   }
   else{
      incrementPageNumber(currentPageCount);//add 1 to the "pageNumber" value
   }
}
function incrementPageNumber(i){
   if (i != 2) { //if the page number is not equal to 2
      i++;  //add one to the page number
      document.cookie="pageNumber="+i;  //and write it to the cookie
     // alert(getCookie("pageNumber")); //for testing remove when live
     
   }
   else{
   //open the pop up window
      if (getCookie("windowOpened") == "false"){ //if the window hasn't already been opened NEW
        window.open('popup.html','advertisement','width=200,height=200');
        document.cookie="windowOpened = true";
        }
   }
}

Two new lines. One sets the initial value of the windowOpened value in the cookie to false. The other checks to see if the value is false before opening the window. It is working for me. Let me know if it works for you.

Wow JT that almost looked like you knew what you were doing!
 
OK here it is. The script worked ok in IE on PC and NS on Mac by placing the document.cookie="windowOpened=true" before the window popped up.

That didn't fix the problem with IE on Mac. I was finally able to figure it out. IE on the Mac apparently has a bug in the way it stores cookies. So while the script was working correctly on all other browsers, the IE browser was writing the following to the cookie
Code:
pageNumber=1;windowOpened=false;windowOpened=true;

Seems like the browser was writing a different cookie for each function. VEEERY messed up behavior.

So here is the fix (tested on IE PC, IE Mac, NS Mac)

Code:
function setPageNumber(i){
	//set window opened to true if pop up has occured.
	if (i == 1){
		document.cookie="windowOpened=1";
	}
	//Insert current page number
	var currentPageCount = getCookie("pageNumber");
	if (getCookie("pageNumber")==null){//if there is no cookie
		document.cookie="pageNumber=1";//create the cookie
		document.cookie="windowOpened=0";//set window opened as false
	}else{
		incrementPageNumber(currentPageCount);//add 1 to the "pageNumber" value
	}
}

function incrementPageNumber(i){
	if (i != 2) { //if the page number is not equal to 2
		i++;//add one to the page number
		document.cookie="pageNumber="+i;//and write it to the cookie
	}else {
		//open the pop up window
		if (getCookie("windowOpened") == 0){ //if the window hasn't already been opened NEW
		
		setPageNumber(1);
		window.open('popup.html','newwin','width=350,height=255,left=0,top=0,toolbar=no,location=no,scrollbars=no,status=no,resizable=no,fullscreen=no');newwindow.focus();void(0);
		}
	}
}

BTW get rid of your extra setPageNumber() function and replace the two functions above. Leave the getCookie function as is.

I look forward to you telling me this did the trick :)


Wow JT that almost looked like you knew what you were doing!
 
So it seems I have another problem.

The javascript uses a onload command to set the cookie. Each one of the pages on the site I need to add this to has a <body onload""> command currently.

It seems the two override each other and the cookie does not get written.

If the body onload feature were only on one page of the site it would be fine, just put the setpagenumber function a page behind, so it would launch on 4 not three.

Unfortunately all pages in the site use the body onload command.

Any ideas?

----------------------------------------
 
What is it currently doing onload? Calling a function?

Wow JT that almost looked like you knew what you were doing!
 
Create a new function. Something like:

Code:
function pageLoad(){
   CSSScriptInit();
   setPageNumber();
}

Change your page load to <body onload="pageLoad();">

I think that will work.

Wow JT that almost looked like you knew what you were doing!
 
not to be a cynic but all the headaches this is causing you would be cured if you could SELL your company on transitioning to a server side script.

zzzzzz
 
That would make it MUCH easier! :)

Wow JT that almost looked like you knew what you were doing!
 
Thanks JT I will give it ago with that , I was going to try that cause it made sense. Just got side tracked.

Deecee trust me I understand the benefits. Everything I do for my personal business/portfolio in the future will be a mixture of Coldfusion sites and PHP, pending my needs.

These sites however are all mostly recks. Static sites built with Go-Live. I walked into this company(my day job) with this in my lap. Their clients will not pay to go dynamic because they dont see a need for it, no matter how much you try to explain, all they see is costs and their eyes begin to glaze over.

It's a loosing battle. I have however begin bringing in coldfusion into all the new stuff I do for them so that is a plus.

Anyways....

----------------------------------------
 
That's a cool link NorthStarDA thanks.

Unfortunately for me the the cost of the server environment is not really the issue, the current hosts they use supports coldfusion so I have that option.

The problem lies in the clients and their willingness to pay the hourly rate to bring their sites around to the twentieth century. Most of these people can not even open emails it seems...

Regardless, thanks everyone...

----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top