Difference between revisions of "Cookie Warning Tutorial"
From Joomla! Documentation
m (Marker template removed because the author is not active)
Latest revision as of 02:17, 24 August 2023
This article or section is incomplete, which means it may be lacking information. You are welcome to assist in its completion by editing it as well. If this article or section has not been edited in several days, please consider helping complete the content.
Suggestions are always welcome!
Just do not directly enter it into the page. Rather switch to the talk page and write down your thoughts. Or send an email to the author. Thanks
In some countries, there are legal requirements to issue a warning on websites when cookies are created. The article Cookie notification explains what a cookie is.
Joomla! automatically creates such cookies to track the session and other information of visitors. Therefore, it might be necessary to provide appropriate notices. The Joomla! Framework itself does not include a feature to display cookie warnings.
A simple option would be to install one of the many Joomla! extensions that offer to present such a warning. extensions.joomla.org is the official Joomla! Extensions DirectoryTM. The word cookie entered into the search field of this page will provide a multitude of free or paid extensions to handle cookies.
The installation of extensions bears certain risks
- What exactly is being installed?
- How does the extension influence the rest of the system?
- Does the extension conflict with other installed extensions?
- How to remove the extension if it does not suit?
- Is there an uninstall function and is it complete or does it leave residues that affect the system?
- Does the extension even do something else that is not wanted?
- How does the extension behave in the next major upgrade?
- Will the extension still be supported in the next version?
- Does the extension cost extra money and how much, once or repeating?
This tutorial assumes that Joomla! Version 4.x (in short J4) is installed and the administrator has a rough overview of the administration interface Atum, which is the default backend template for J4. (This tutorial should also be suitable for older Joomla versions, but is not tested in this context.)
Even though there are not so many differences in the procedure, this tutorial assumes that no extensions are installed. This means that the end user interface is Cassiopeia, the default site template for end users in Joomla! 4.
Presumably there is already a main menu and a few articles. (In a very fresh test installation the sample data could be installed.)
Beginners don't have to worry, everything that is created can be deactivated and also deleted with a few mouse clicks. That way restoring the original state and leaving no residues.
Browser specifics in this tutorial refer to FireFox in a fairly current version.
So no more foreplay, let's get started.
Create a Site Module for the warning message
In administrator interface click Contents and besides Site Modules click the + sign. Or click Site Modules and then click New. This starts creation of a new module. (A module is a small component, which generates output on a Joomla! web page. A Site Module creates output for the end users. An Administrtor Module would create outbut in the administration interface.)
- Click Custom to create a Custom Site Module.
- Enter a Module Title (for Example: Cookie Warning).
- In the editor field write a message. Let's start with: By using this site you accept cookies! (We are going to enhance this later.)
- To define where to display the warning in the web page select the Position main-top. (That would be right under the page header before the content.)
- Select to publish the warning. (If later anything gets messed up, just unpublish the module. Only published components are seen by end users.)
- Leave everything else in it's default settings and click Save & Close at the top of the page.
That's it. We have our warning. After opening the site view of the Joomla Website in a new browser window the warning should be displayed.
The way the Cookie Warning works by now is, that it is shown on every page. So lets add a button to hide the message.
- Open the Site Module again under Content -> Site Modules -> Cookie Warning.
- Below the editor field click the button Toggle Editor to switch the editor to display the HTML source code of the cookie warning.
- Notice the text in the editor field: <p>By using this site you accept cookies!</p>. (The surrounding <p> and </p> tags define the text as a paragraph in HTML.)
- Add a Return or new line at the end of the existing code. Maybe add another one to make it look better.
- Now copy the following code snippet below the existing text of the message.
<button class="btn btn-primary" type="button" onclick="
if (document.cookie == 'cookies=accepted')
We will discuss later what this code exactly is doing.
- Now click Toggle Editor again and the I accept button should be visible. (If it looks ugly, the editor is not using Bootstrap.)
- Click Save at the top of the page to store the change without closing the Modules: Custom page.
- Now switch to the browser window showing the site view and refresh. You should not only see the warning, but also the button. (Refreshing may be done by clicking the circular icon or by pressing the F5 key. If it does not help try pressing Ctrl-F5.)
After clicking the button ...
Debugging the Cookie Warning
What happend? Nothing? Shouldn't the warning disappear when the button is clicked?
Before debugging, take a brief look at what the code is doing
- There is a HTML button tag with the attribute class="btn btn-primary" (these classes are used by bootstrap to format a nice button in the primary color).
- Following is an onclick="" attribute.
- The first statement finds the HTML Tag with the ID #cookieWarning and gives it a CSS style of display: none; (it is in single quotes, because the double quotes are already used by the onclick="" surrounding it.
- The second statement sets a cookie in the clients browser with the name cookies and the content accepted.
- Last, the button gets a label of I accept and the HTML closing tag for the button.
So when the button is clicked by the end user, it finds the container of the Cookie Warning by looking for its ID, hides this from the user and then sets a marker in the users browser to remember that he accepted the cookies.
- The opening HTML <script> tag.
- An if statement with a test in the round brackets ().
- It tests for a cookie in the cache of the end users browser which is named cookies and has a content of accepted.
- If that cookie exists the test result ist positive (true) and the commands in the curly brackets are executed.
- In the curly braces is a command we already know. Hide the Cookie Warning.
- The closing HTML </script> tag.
So what all of this is doing: If there is no such cookie in the end users browser, it displays the warning on every page displayed. If somewhen the user gets bored and clicks the button it hides the warning and remembers to hide it every time the end user is visiting the website.
In the browser open the developer tools. In FireFox that is done with the key combination: Shift+Ctrl+I
At the very top of the debuging window select the Inspector tab and search for Cookie Warning. Check if the title, the message, the button and the script tag are all there. Should be fine.
- Again at the very top of the debuging window select the Storage tab.
- Open the Cookies file
- Open your domain name, it should be there.
- In the content area there should be at least 2 cookies. One is the Joomla! cookie for tracking your session. The other should be the cookie just set by clicking the I accept button.
Everything is fine until now.
- Switch back to the Inspector tab of the debugging tools.
- Enter #cookieWarning into the search field and press Return to jump to the Cookie Warning.
-- Ups, an Error! OK, let us try to enter Cookie Warning into the search field again. and analyse the HTML.
All kinds of classes, but no ID named cookieWarning!
That is easy to solve:
- Open up the Site Module Cookie Warning in the administator window again, if it is not still open.
- Toggle the editor to see the HTML code.
- Notice that the editor added some more <p>...</p> tags. No problem, if it likes to see everything as a paragraph.
- At the very top insert the following: <div id="cookieWarning">
- Right above the <script> tag insert: </div>
- So we gave our HTML a surrounding division tag with an ID of #cookieWarning.
- Save the changes.
- Refresh your browser end user browser window.
Now it should work! Great!
A big warning
There are templates out there which do not take all those attributes and convert it into HTML. That means, if you are using such templates you might get alot of trouble debugging your code and the template is just not showing it to the browser.
If you have a add-on template installed always have a hidden menu by hand (a menue which is defined and has at least one Menu Item), but which is not connected to any Template Position. (And therefore not shown on the website) Then have this Menu Item displayed in the Cassiopeia template. By typing the Alias of this Menu Item behind the domain name in the browsers address field, it is always possible to access this testpage with Cassiopeia template for debugging.