Jump to content


Photo

Javascript version of this php line


  • Please log in to reply
4 replies to this topic

#1 toolman

toolman

    Advanced Member

  • Members
  • PipPipPip
  • 168 posts

Posted 15 February 2013 - 08:07 AM

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

#2 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,781 posts
  • LocationBonita, FL

Posted 15 February 2013 - 08:56 AM

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.


Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#3 toolman

toolman

    Advanced Member

  • Members
  • PipPipPip
  • 168 posts

Posted 15 February 2013 - 09:04 AM

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

#4 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,781 posts
  • LocationBonita, FL

Posted 15 February 2013 - 09:15 AM

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

Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#5 toolman

toolman

    Advanced Member

  • Members
  • PipPipPip
  • 168 posts

Posted 15 February 2013 - 09:20 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com