Вопрос

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.

Это было полезно?

Решение

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: '/'});
}

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top