Jump to content

change content based on a cookie already set


toolman

Recommended Posts

Hi there,

 

I have a page which displays some text based on a cookie that is set. What I would like to do is to over-ride that text/html by using an if statement based on the cookie value.

 

I have the following, but not having any luck with it:

if ($.cookie('cookie_name')=='cookie_value'){
alert("true");
$("#box").html("new text");
}

 

Ideally, I'd like to use JavaScript rather than jQuery due to limitations.

 

Any ideas?

Thanks!

Link to comment
Share on other sites

This is really vague.

 

What exactly is the problem? Are you getting an error? A false positive? A false negative? What have you tried to debug the problem?

 

And what are the “limitations” you're dealing with? If you think jQuery is too heavy, there are plenty of lightweight libraries specifically for cookies. Manually parsing document.cookie sucks, so you'll want to avoid that.

Link to comment
Share on other sites

Thanks for the reply. After posting this, I realised, I may not have jQuery cookie plugin installed.

 

Basically, a page loads with a URL for example of: http://www.website.com/cookie_name

 

This then inserts a cookie called cookie_name with a value of cookie_value

 

E.G: http:www.website.com?agent=john

 

This then gives the cookie "cookie_name" a value of "john"

 

What I would like to do is to use an if statement based on if the cookie has a value of "john" to do something; replace a one line of HTML.

 

Not sure if that helps?

Link to comment
Share on other sites

The website has over 100,00 pages - not all have a global library with jQuery included, so I wanted to see if I could use native JavaScript. Initially I thought jQuery was included on all pages, but it's not. I have a JS include globally, but because the website is so old, I need to work with something without jQuery if possible. 

Link to comment
Share on other sites

As I told you multiple times, jQuery is not required. The above library is a single JavaScript file with 150 lines of code. You can copy-and-paste those straight into your HTML markup or wherever you want it.

 

Hell, you could even strip the code of everything but the get() method. Then you're at 40 lines of code. What more do you need?

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.