I have a problem regarding the topic " Cookies" in Page 325 of Chapter 20,
The cookies consent message still show after click OK and refresh the web page.
and even check in both safari and google chrome browser > setting page to enable javascript or enable cookie .
Below are the code of javascript and route handler.
In Public/scripts/cookies.js
function cookiesConfirmed() {
$(’#cookie-footer’).hide();
var d = new Date();
d.setTime(d.getTime() + (3652460601000));
var expires = “expires=”+ d.toUTCString();
document.cookie = “cookies-accepted=true;” + expires;
}
The route handler in websiteController.swift
func indexHandler(_ req: Request) throws → Future {
return Acronym.query(on:req).all().flatMap(to: View.self) {
acronyms in
let acronymsData = acronyms.isEmpty ? nil: acronyms
let userLoggedIn = try req.isAuthenticated(User.self)
let showCookieMessage = req.http.cookies[“cookies-accepted”] == nil
let context = IndexContext(title: “Homepage”,acronyms: acronymsData ,userLoggedIn: userLoggedIn,showCookieMessage: showCookieMessage)
return try req.view().render(“index” , context)
First problem is that you copied the js from the book. That’s often a problem with code from books, the publishing software replaces quotes we use in software with typographical quotes. If you copy them into your code, you’ll get a syntax error.
$(’#cookie-footer’).hide();
^ ^ those should be ' like this:
$('#cookie-footer').hide();
I have no idea about the Uncaught ReferenceError though. Please post the part of base.leaf that’s inside the #if(showCookieMessage) {} block.