Jump to content

how to set cookies with hyperlink


dreamwest

Recommended Posts

how can i set a cookie with a simple hyperlink and reload() function, without a popup message saying confirm .

 

eg. user clicks a hyperlink and automatically the page gets reloaded and cookie set - no popup message

 

An example of the cookie script i have now - popup version:

 

<a href="javascript: set_cookie('customize', 'on');"><script>function set_cookie(object, value) { if (value == 'off') {	var message = 'off' }	else { var message = 'on'
} if(confirm(message)) { var expireDate  = new Date()
var expstring   = expireDate.setDate(expireDate.getDate() + 500)
document.cookie = object+'='+value+'; expires='+expireDate.toGMTString()+'; path=/';
window.location.reload() } }</script>Dont show this message again</a>

Link to comment
Share on other sites

Your code should be less hard to read if you add some structure:

 

<!-- JavaScript should be in the header -->

<head>
<script type="text/language">
function set_cookie(object, value) { 
    if (value == 'off') {   
        var message = 'off';
    }
   
    else { 
        var message = 'on';
    } 

    if(confirm(message)) { 
        var expireDate  = new Date();
        var expstring   = expireDate.setDate(expireDate.getDate() + 500);
        document.cookie = object + '=' + value + '; expires = ' + expireDate.toGMTString() + '; path=/';
        window.location.reload();
    } 
}
</script>
</head>

<body>
<a href="javascript:void(0);" onclick="set_cookie('customize', 'on')">Dont show this message again</a>
</body>

Link to comment
Share on other sites

im trying to set up this simple cookie script but its a bit buggy - can anyone improve this?

 

<head><script>function set_cookie(object, value) { if (value == 'off') {	var message = 'off' }	else { var message = 'on'
} if(confirm(message)) { var expireDate  = new Date()
var expstring   = expireDate.setDate(expireDate.getDate() + 500)
document.cookie = object+'='+value+'; expires='+expireDate.toGMTString()+'; path=/';
window.location.reload() } }</script></head>
<?php 
if (isset($_COOKIE['customize'])) 
echo "";
else
echo "";
if  ($_COOKIE['customize'] == 'off') 
echo "<a href=\"javascript: set_cookie('customize', 'on');\">turn on message</a>";

else
echo "<a href=\"javascript: set_cookie('customize', 'off');\">turn off message</a>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.