Question

I am a bit confused and might be missing something in front of my face.

I am using jQuery cookie to create a cookie in my page. The code is:

var $j = jQuery.noConflict();
$j(document).ready(function(){
$j.cookie("homecookie", 1, {expires: 30, path: '/'});
});

I know to read the cookie it would be $j.cookie('homecookie'); but after that is where I get confused. I need to call this function $j.colorbox({ inline:true, href:"#gallery-nav-instruct"}); How?

In other words if the person has not visited the page then colorbox is called. If the user has visited the page then it is not called. Can someone help me set this up properly as I cannot get it. Please give examples as javascript is not my cup of tea.

Was it helpful?

Solution

You can simply check for the presence of the cookie, if it is not present you can call function and set the cookie:

if(!$j.cookie('homecookie')){
    $j.colorbox({ inline:true, href:"#gallery-nav-instruct"});
    $j.cookie("homecookie", 1, {expires: 30, path: '/'});
}

OTHER TIPS

Well just check the cookie value:

if (!$j.cookie('homecookie')) $j.colorbox({ /* whatever */ });

Or if the exact cookie value is important, compare:

if ($j.cookie('homecookie') !== 'something')
  $j.colorbox({ /* whatever */ });

In both those examples, I'm assuming that you want to call the colorbox thing when the cookie is not what it should be when some user has never visited.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top