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

  1. Install the Modal Page module
  2. Configure a modal page
  3. Ensure the js-cookie library is not loaded or fails to load
  4. Load a page that should display the modal
  5. Open browser console
  6. 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:

  1. Line 60: Add check before calling cookies.remove()
  2. Line 64: Add checks before calling cookies.get() (both calls)
  3. Line 91: Add check before calling cookies.set() in the hide event handler
  4. 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.

Comments

istankevych created an issue. See original summary.

istankevych’s picture

renatog’s picture

Very nice catch!

It makes sense for me

And I'm agree about the verification before using to avoid unexpected behaviours

renatog’s picture

Status: Active » Reviewed & tested by the community
Parent issue: » #3553252: Plan for the next Modal Releases

Marking as RTBC and creating a plan for the next release

Thank you so much @istankevych

kenfordesign’s picture

I am seeing this same behavior on version 5.1.8. Unfortunately this patch does work for that version. Any suggestions?

renatog’s picture

Any suggestions?

@kenfordesign could you enable "js-cookie" ?

I think it'll fix the issue for now

  • e2a923b2 committed on 6.0.x
    feat: #3564556 JavaScript error when js-cookie library is not loaded
    
    By...
renatog’s picture

Status: Reviewed & tested by the community » Fixed

Thank you so much for your contribution @istankevych

Moved to the dev branch (6.0.x)

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

renatog’s picture

Released and added public thanks in the release notes: https://www.drupal.org/project/modal_page/releases/6.0.0-beta13

renatog’s picture

I am seeing this same behavior on version 5.1.8

Just created an issue to apply this fix on 5.1.x: #3579357: [5.1.x] JavaScript error when js-cookie library is not loaded

renatog’s picture

Fixed 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

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.