Hello, In need of some help with my JavaScript. What it is, I have JavaScript code that lets visitors to my websites know that I am putting cookies on their machine, it's EU law. The code I've got at the minute (which is shown below) is giving me a 'Uncaught TypeError: Cannot set property 'innerHTML' of null' error in my Google Chrome Console. Although the script still works fine I would rather my website had no errors. I've highlighted the error causing section of the code below, if anyone knows JavaScript and can help me figure out a patch I'd be made up! Thank you for your time. function getCookie(c_name) { var i, x, y, ARRcookies = document.cookie.split(";"); for (i = 0; i < ARRcookies.length; i++) { x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("=")); y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1); x = x.replace(/^\s+|\s+$/g, ""); if (x == c_name) { return unescape(y) } } } function displayNotification() { var message = "<div id='cookiewarning'><div id='inner'>"; message = message + "<div id='message'>We use cookies to track visits to our website. By using our website you accept our use of cookies.</div>"; message = message + "<input type='button' class='submit' value='What are cookies?' onclick='window.open(\"http://www.allaboutcookies.org/\")' /><input type='button' value='Dismiss' class='submit' onclick='JavaScript:setCookie(\"jsCookieCheck\",null,365);' />"; message = message + "</div></div>"; document.writeln(message) } [B]function setCookie(c_name, value, exdays) {[/B] [B] var exdate = new Date();[/B] [B] exdate.setDate(exdate.getDate() + exdays);[/B] [B] var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());[/B] [B] document.cookie = c_name + "=" + c_value;[/B] [B] var cw = document.getElementById("cookiewarning");[/B] [B] cw.innerHTML = ""[/B] [B]}[/B] function checkCookie() { var cookieName = "jsCookieCheck"; var cookieChk = getCookie(cookieName); if (cookieChk != null && cookieChk != "") { setCookie(cookieName, cookieChk, 365) } else { displayNotification() } } checkCookie(); Code (markup): Thanks again.
It means cookiewarning doesn't seem to exist when your function is called -- most likely this line: var cw = document.getElementById("cookiewarning"); is returning null -- are you sure displayNotification is being run BEFORE setCookie? checkCookie seems to be calling EITHER setCookie or displayNotification, when to function PROPERLY setCookie needs to be called AFTER displayNotification. Really though, you probably shouldn't be using innerHTML or document.write and should be manipulating the DOM to do whatever it is you are trying to do here. Either way, I'm having a dog of a time trying to make sense out of what this mess of outdated code is even trying to accomplish.... what with the INPUTs that are not in a form, INPUT doing anchor's job, document.write either doing static markup or DOM manipulation's job, bizarro-land getcookie method, single instance (I think) function not referencing itself... Much less why you'd be dicking with cookies so much or even having that disclaimer in the first place...
Thanks for the reply! I'm clueless with JavaScript, I found the code a couple of weeks back, I used it because it worked lol. The cookie warning displays at the top of my websites with two buttons, the dismiss button makes the notice disappear and stay off the website until the cookies are cleared from the browser. The other button just links. Do you think it is best for me to find another script or is this fixable?
Honestly, that's ALL crap I wouldn't even be wasting bandwidth doing on a website in the first place -- oh noes, it uses cookies, so does EVERY SINGLE FORUM OR SITE YOU CAN POST RESPONSES TO... and do you see them pulling those types of 'gee ain't it neat' scripting stunts? No you do not. I'd swing an axe at all of it. Waste of code, waste of time, waste of effort. You shouldn't be doing anything that would warrant code like that in the first place. But I'm a firm believer that the only cookies that should really be 'set' on a modern site is a single PHP session ID or DB driven session hash. The rest of it is a waste of time, bandwidth, and just slows down the site it's on.
Ok, I'll take that notice down and just add a line into the privacy policy then. Thanks for the time replying.
That's the sane approach -- it's very easy -- WAY too easy in fact, to get lured into "gee ain't it neat" scripting like that to do something which can simply be handled by a line or two in your disclaimer. In general MOST of the scripting people put on websites falls into that category -- and we ALL do it when we start out. The question you always have to ask is "How does this help the user get to my content?" -- if it does nothing apart from get in the way (like this script did) or some silly visual effect (see 90%+ of what people do in jquery) the answer is it doesn't, and as such ends up little more than a waste of bandwidth and a means to drive users AWAY from your site. Which much like the 'truth' of graphical design and websites, can be a bitter pill to swallow ESPECIALLY when just starting out. Simple truth is that people visit websites for the CONTENT, NOT the goofy graphics you hang around it and NOT any goofy animations or extra scripting that gets in the way of that content -- I know I had a hard time of it, my first 'real' website some decade and some change ago being an inaccessible DHTML nightmare because I copypasta'd pretty much every goofy effect there was on Dynamic drive and had almost ALL the markup built by js. Looking back I often wish someone had taken the time to tell me "No" -- which is why I spend so much time going around telling others "No" as they start down that same path.