Nar Nar - 2 years ago 192
Javascript Question

Getting a Uncaught TypeError: Cannot read property 'split' of undefined Error on specific pages only

So, I've been using iziModal and I'm getting an error on specific pages that's related to iziModal. The full error code is:


Uncaught TypeError: Cannot read property 'split' of undefined
at r.fn.init.t.fn.(anonymous function) [as iziModal] (https://cdnjs.cloudflare.com/ajax/libs/izimodal/1.5.0/js/iziModal.min.js:6:22531)
at http://localhost:8000/js/scripts.js:3:17


scripts.js:3:17 on that error points to:

$('#userPanel').iziModal({
title: 'View User Data',
subtitle: 'View and update each employee\'s records in the database',
icon: 'glyphicon glyphicon-user',
padding: '15',
fullscreen: true
});


It's causing my ajax and jquery scripts on the affected pages to not work. My script order is right as well.

Answer Source

It seems that pages doesn't contain element with id #userPanel. you can check if element exists before execute code:

if($('#userPanel').length) {
  $('#userPanel').iziModal({
    title: 'View User Data',
    subtitle: 'View and update each employee\'s records in the database',
    icon: 'glyphicon glyphicon-user',
    padding: '15',
    fullscreen: true
  });
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download