Setting Multiple cookies with one onclick action
mcotterill
Status: New User - Welcome
Joined: 02 Dec 2008
Posts: 1
Reply Quote
I have the following code, which works fine if I am setting one cookie, but does not set any cookies if I add subsequent Set_Cookie statements to the DoAllThese function.

Can someone please point out what is going wrong?

<script language="javascript">
/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
// if the expires variable is set, make the correct expires time, the
// current script below will set it for x number of days, to make it
// for hours, delete * 24, for minutes, delete * 60 * 24
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
var expires_date = new Date( today.getTime() + (expires) );
//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
function DoAllThese() {
Set_Cookie( "theme", "themeTitle" );
Set_Cookie( "tDecode", "themeDecode" );
Set_Cookie( "tExpress", "themeExpressions" );
Set_Cookie( "tMeaning", "themeMeanings" );
}
</script>

<div class="Normal">
<a class="decoderButton" href="/DreamThemes/tabid/619/xmmid/1739/xmid/<xmod:recordid />/Default.aspx" onclick="DoAllThese()">Add This Theme to my Dream Decoder!</a><br><br>
</div>

Thanks,
Marney
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
Every time someone posts a question using this type of non-standard aspx code I have to really wince with pain.

Since there are non-standard proprietary things going on there, and what else is new with microsoft and the internet, it's really not possible to make any further statements.

I suggest you do the following: remove all aspx code, create a simple plain html page, with the javascript you showed above., then add a simple, clean, straight html onclick event to a link, plain, uncluttered, then test to see if the cookies all set.

Once you're removed the proprietary aspx stuff from the equation you might actually be able to debug the question further, but not until then.
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
Besides the issues of unpredictable proprietary aspx methods, there's a few basic tests, run, as noted above, on a CLEAN, straight HTML page, with just the javascript and links, nothing else.

Make the following change and I suspect you'll find where the cookies fail:

:: Code ::
function DoAllThese() {
Set_Cookie( "theme", "themeTitle" );
Set_Cookie( "tDecode", "themeDecode" );
Set_Cookie( "tExpress", "themeExpressions" );
Set_Cookie( "tMeaning", "themeMeanings" );
alert('we got here!');
}


My guess is the page is moving away before the 3 other cookies have time to set.

The solution to that would be to redirect the page only after the cookies have set, that's my guess, but I also agree with the futility of trying to debug this issue until it's in clean pure javascript and html.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours