Skip to content

Commit a41eff0

Browse files
authored
Merge pull request #49472 from shurup/fix-banner-dismiss-js
Fix JavaScript null object error in banner-dismiss.js
2 parents de4903d + 2c59879 commit a41eff0

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

assets/js/banner-dismiss.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@ $(document).ready(function() {
2121

2222
/* Check the presence of a cookie */
2323
let announcement = document.querySelector("#announcement");
24-
let token = `announcement_ack_${announcement.getAttribute('data-announcement-name').replace(/\s/g, '_')}`; // Generate the unique token for announcement
25-
let acknowledged = getCookie(token);
26-
if (acknowledged === "true") {
27-
announcement.remove(); // Remove the announcement if the cookie is set
28-
}
29-
else {
30-
announcement.classList.add('display-announcement') // Display the announcement if the cookie is not set
31-
}
24+
if (announcement) {
25+
let token = `announcement_ack_${announcement.getAttribute('data-announcement-name').replace(/\s/g, '_')}`; // Generate the unique token for announcement
26+
let acknowledged = getCookie(token);
27+
if (acknowledged === "true") {
28+
announcement.remove(); // Remove the announcement if the cookie is set
29+
}
30+
else {
31+
announcement.classList.add('display-announcement') // Display the announcement if the cookie is not set
32+
}
33+
}
3234

3335
/* Driver code to set the cookie */
3436
let button = document.querySelector('#banner-dismiss');
35-
button.removeAttribute('style');
36-
button.addEventListener('click', function() {
37-
setCookie(token, "true",
38-
button.getAttribute('data-ttl')); // Set a cookie with time to live parameter
39-
announcement.remove();
40-
});
37+
if (button) {
38+
button.removeAttribute('style');
39+
button.addEventListener('click', function() {
40+
setCookie(token, "true",
41+
button.getAttribute('data-ttl')); // Set a cookie with time to live parameter
42+
announcement.remove();
43+
});
44+
}
4145
});

0 commit comments

Comments
 (0)