Problem/Motivation
The modal-page.js file throws a JavaScript error when the js-cookie library (exposed as window.Cookies) is not loaded or available:
Uncaught TypeError: Cannot read properties of undefined (reading 'remove') at HTMLDivElement.<anonymous> (modal-page.js:60:19)
This occurs because the code attempts to call methods on the cookies object without first checking if it exists. The cookies parameter is passed from window.Cookies, which may be undefined if:
- The js-cookie library hasn't been loaded yet
- The library failed to load
- The library is not included in the page
This error can break other JavaScript on the page and prevent the modal from functioning properly.
Steps to reproduce
- Install the Modal Page module
- Configure a modal page
- Ensure the js-cookie library is not loaded or fails to load
- Load a page that should display the modal
- Open browser console
- Observe the JavaScript error:
Cannot read properties of undefined (reading 'remove')
Proposed resolution
Add null/undefined checks before using the cookies object in all locations where it's accessed. This ensures the code gracefully handles the case when the js-cookie library is not available.
Code changes needed:
- Line 60: Add check before calling
cookies.remove() - Line 64: Add checks before calling
cookies.get()(both calls) - Line 91: Add check before calling
cookies.set()in the hide event handler - Line 202: Add check before calling
cookies.set()in the click handler
// Remove the cookie if show only once option is disabled. - if (show_once != "1") { + if (show_once != "1" && cookies) { cookies.remove('hide_modal_id_' + id_modal); } // Get cookies for do not show again option and show only once. - var hide_modal_cookie = cookies.get('hide_modal_id_' + id_modal) || cookies.get('please_do_not_show_again_modal_id_' + id_modal); + var hide_modal_cookie = (cookies && cookies.get('hide_modal_id_' + id_modal)) || (cookies && cookies.get('please_do_not_show_again_modal_id_' + id_modal)) || null; modal.on('hide.bs.modal', function () { - if (show_once == "1") { + if (show_once == "1" && cookies) { cookies.set('hide_modal_id_' + id_modal, true, { expires: 365 * 20, path: '/' }); } }); ok_button.on('click', function () { - if (checkbox_please_do_not_show_again.is(':checked')) { + if (checkbox_please_do_not_show_again.is(':checked') && cookies) { // ... cookie setting code } });
Expected behavior after fix:
The code should gracefully handle the case when the cookies object is undefined. Cookie functionality should be skipped if the library is not available, but the modal should still function without throwing errors.
Remaining tasks
- Review and test the proposed fix
- Ensure backward compatibility (modal should still work when cookies library is available)
- Update documentation if needed
User interface changes
None. This is a bug fix that prevents JavaScript errors. No UI changes are required.
API changes
None. No API changes are required.
Data model changes
None. No data model changes are required.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | 3564556-fix-cookies-undefined-error-2.patch | 1.5 KB | istankevych |
Comments
Comment #2
istankevych commentedComment #3
renatog commentedVery nice catch!
It makes sense for me
And I'm agree about the verification before using to avoid unexpected behaviours
Comment #4
renatog commentedMarking as RTBC and creating a plan for the next release
Thank you so much @istankevych
Comment #5
kenfordesign commentedI am seeing this same behavior on version 5.1.8. Unfortunately this patch does work for that version. Any suggestions?
Comment #6
renatog commented@kenfordesign could you enable "js-cookie" ?
I think it'll fix the issue for now
Comment #8
renatog commentedThank you so much for your contribution @istankevych
Moved to the dev branch (6.0.x)
Comment #10
renatog commentedReleased and added public thanks in the release notes: https://www.drupal.org/project/modal_page/releases/6.0.0-beta13
Comment #11
renatog commentedJust created an issue to apply this fix on 5.1.x: #3579357: [5.1.x] JavaScript error when js-cookie library is not loaded
Comment #12
renatog commentedFixed at #3579357: [5.1.x] JavaScript error when js-cookie library is not loaded and released the new version with this fix: 5.1.9
Thanks team