toolman Posted February 15, 2013 Share Posted February 15, 2013 Hi, I have this PHP line, but is there a very similar, simple Javascript way of doing it? <? if(!isset($_COOKIE["eucookie"])) { ?> content <?php } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/274525-javascript-version-of-this-php-line/ Share on other sites More sharing options...
kicken Posted February 15, 2013 Share Posted February 15, 2013 To test if a cookie is set or not you'd use this: if (document.cookie.indexOf('eucookie=') == -1){ //cookie is not set } else { //cookie is set } You can't conditionally include/exclude blocks of HTML with JS like you can with PHP though (by just wraping it in an if). You'd have to convert the HTML into a JS string and use document.write or do something onload. Quote Link to comment https://forums.phpfreaks.com/topic/274525-javascript-version-of-this-php-line/#findComment-1412612 Share on other sites More sharing options...
toolman Posted February 15, 2013 Author Share Posted February 15, 2013 Thanks. I now have this <script> if (document.cookie.indexOf('eucookie=') == -1){ //cookie is not set document.write('hello'); } else { //cookie is set document.write('bye'); } </script> <a href="javascript:" onclick="SetCookie('eucookie','eucookie','time()+31556926')"> I am trying to create the cookie when the link is clicked, but it isn't working Any ideas why? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/274525-javascript-version-of-this-php-line/#findComment-1412613 Share on other sites More sharing options...
kicken Posted February 15, 2013 Share Posted February 15, 2013 Did you create/import a setcookie() function with javascript, or are you trying to call PHP's setcookie function? If the latter then, you can't do it like that, you need to use ajax or reload the page (ie, use a normal link to a .php file, not onclick). Either way, the value you're passing as the third parameter is almost surely wrong. If you're trying to use PHP's setcookie, you need to remove the quotes around the third parameter. If you have a JS function then it probably would be expecting a Date object, read the docs for the function. Lastly setting the cookie isn't going to cause the if to be re-evaluated and appear right away, you'd need to reload the page to see it change. An Example Quote Link to comment https://forums.phpfreaks.com/topic/274525-javascript-version-of-this-php-line/#findComment-1412615 Share on other sites More sharing options...
toolman Posted February 15, 2013 Author Share Posted February 15, 2013 I see, thanks for your help. What I am trying to do is hide a div that appears on the page so when a user closes the div, it no longer displays for a set amount of time. Would that be possible? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/274525-javascript-version-of-this-php-line/#findComment-1412616 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.