toolman Posted June 29, 2016 Share Posted June 29, 2016 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! Quote Link to comment https://forums.phpfreaks.com/topic/301415-change-content-based-on-a-cookie-already-set/ Share on other sites More sharing options...
Jacques1 Posted June 29, 2016 Share Posted June 29, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301415-change-content-based-on-a-cookie-already-set/#findComment-1534116 Share on other sites More sharing options...
toolman Posted June 29, 2016 Author Share Posted June 29, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301415-change-content-based-on-a-cookie-already-set/#findComment-1534117 Share on other sites More sharing options...
Jacques1 Posted June 29, 2016 Share Posted June 29, 2016 I understand what you're trying to do. What I do not understand is why you cannot or don't want to install a library. The js-cookie library has merely 150 lines of code. Put it on your server, adjust the above code, and you'll be done in 5 minutes. Quote Link to comment https://forums.phpfreaks.com/topic/301415-change-content-based-on-a-cookie-already-set/#findComment-1534118 Share on other sites More sharing options...
toolman Posted June 29, 2016 Author Share Posted June 29, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301415-change-content-based-on-a-cookie-already-set/#findComment-1534119 Share on other sites More sharing options...
Jacques1 Posted June 29, 2016 Share Posted June 29, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301415-change-content-based-on-a-cookie-already-set/#findComment-1534120 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.