Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: $(...).size is not a function

I'm using a plugin to insert an image from the file manager, however I'm getting an error:

imagebrowser.php?func=addImage&w=160&h=120:77 Uncaught TypeError: $(...).size is not a function
  at addImage (/site/dashboard/load.php?id=i18n_gallery&create:564)
  at submitLink (imagebrowser.php?func=addImage&w=160&h=120:77)
  at HTMLAnchorElement.onclick (imagebrowser.php?func=addImage&w=160&h=120:53)
  addImage @ /site/dashboard/load.php?id=i18n_gallery&create:564
  submitLink @ imagebrowser.php?func=addImage&w=160&h=120:77
  onclick @ imagebrowser.php?func=addImage&w=160&h=120:53

The JavaScript code I'm using:

    <p><em><b><?php echo count($filesSorted); ?></b> <?php i18n('TOTAL_FILES'); ?> (<?php echo fSize($totalsize); ?>)</em></p>
  <p><a href="javascript:void(0)" onclick="submitAllLinks()"><?php i18n('i18n_gallery/ADD_ALL_IMAGES'); ?></a></p>
  <?php // foreach ($metadata as &$m) if (!@$m['title']) $m['title'] = basename($m['url']); ?>
  <script type='text/javascript'>
    // <![CDATA[
    var metadata = <?php echo json_encode($metadata); ?>;
    function submitLink(i) {
      var item = metadata[i];
      if(window.opener){
        window.opener.<?php echo $func; ?>(item['url'], item['size'], item['width'], item['height'], item['title'], item['tags'], item['description']);
        <?php if ($autoclose) { ?>window.close();<?php } ?>
      }
    }
    function submitAllLinks() {
      for (var i=0; i < metadata.length; i++) {
        submitLink(i);
      }
    }
    // ]]>
  </script>

EditExtra.php

<?php
  global $id, $url, $parent;
?>
<script type="text/javascript">
  // <![CDATA[
<?php
  $pages = return_i18n_pages();
  # tags
  $tags = array();
  foreach ($pages as $page) {
    foreach (preg_split('/\s*,\s*/', trim(@$page['tags'])) as $t) $tags[$t] = true;
  }
  $tags = array_keys($tags);
  sort($tags);
  # structure
  $structure = return_i18n_page_structure(null,false,$url);
  $siblings = @$pages[''.$parent]['children'];
  if ($siblings) $myindex = array_search($url, $siblings); else $myindex = false;
  $after = $myindex !== false && $myindex > 0 ? $siblings[$myindex-1] : '';
?>
  var after = <?php echo json_encode($after); ?>;
  function changeParent() {
    $parent = $('#post-parent').val();
<?php
  $firstoption = '<option value="0">-- '.i18n_r('i18n_navigation/TOP').' --</option>';
  $page = $pages[null];
  if (isset($page['children']) && count($page['children']) > 0) {
    $children = $page['children'];
    $options = '';
    for ($i=0; $i<count($children); $i++) {
      if ($children[$i] != $url) $options .= '<option value="'.$children[$i].'">'.$pages[$children[$i]]['title'].'</option>';
    }
?>
    if ($parent == null || $parent == '') {
       $('#post-menu-order').empty().html(<?php echo json_encode($firstoption.$options); ?>);
<?php
  }
  foreach ($structure as $page) {
    $page = $pages[$page['url']];
    if (isset($page['children']) && count($page['children']) > 0) {
      $children = $page['children'];
      $options = '';
      for ($i=0; $i<count($children); $i++) {
        if ($children[$i] != $url) $options .= '<option value="'.$children[$i].'">'.$pages[$children[$i]]['title'].'</option>';
      }
?>
    } else if ($parent == <?php echo json_encode($page['url']); ?>) {
       $('#post-menu-order').empty().html(<?php echo json_encode($firstoption.$options); ?>);
<?php
    }
  }
?>
    } else {
       $('#post-menu-order').empty().html(<?php echo json_encode($firstoption); ?>);
    } 
    $('#post-menu-order').val(after);     
  }
  function changeUrl() {
    var url = $('#post-id').val();
    if ($('div.leftopt').length > 0) { // GetSimple 3.0+
      var hide = url.indexOf('_') >= 0;
      if (hide) {
        $('#post-parent').closest('p').hide();
        $('#post-private').closest('p').hide();
        $('#post-template').closest('p').hide();
        $('#post-menu-order').hide();
        $('#post-menu-order').prev().hide();
      }
    } else {
      if (url.indexOf('_') >= 0) {
        $('#post-parent').closest('tr').attr('style','display:none');
        $('#post-private').closest('td').children().attr('style','display:none');
        //$('#post-menu-enable').attr('style','display:none');
        $('#post-menu').nextAll().attr('style','display:none');
      } else {
        $('#post-parent').closest('tr').attr('style',null);
        $('#post-private').closest('td').children().attr('style',null);
        //$('#post-menu-enable').attr('style',null);
        $('#post-menu').nextAll().attr('style',null);
      }
    }
  }
  var tags = <?php echo json_encode($tags); ?>;
  $(function() {
    $fParent = $('#post-parent');
    $fParent.empty();
    $fParent.append(<?php echo json_encode('<option value="" '.($parent == null ? 'selected' : '').'>-- '.i18n_r('NONE').' --</option>'); ?>);
<?php
  foreach ($structure as $page) {
?>
    $fParent.append(<?php echo json_encode('<option value="'.$page['url'].'" '.($parent == $page['url'] ? 'selected' : '').'>'.($page['level'] > 0 ? str_repeat("&nbsp;",5*$page['level']-2).'&lfloor;&nbsp;' : '').cl($page['title']).'</option>'); ?>);
<?php
  }
?>
    if ($('#tick').length > 0) { // GetSimple 3.1+
      $('a.viewlink').hide();
      $('#menu-items').css('height','auto').css('padding-bottom','10px');
      $('#post-menu').attr('style','').addClass('short');
      $('#post-menu').prev().remove();
      $('#post-menu').prev().remove();
      $('#post-menu').prev().remove();
      $('#post-menu').before('<span style="text-transform:none"><label for="post-menu">' + <?php echo json_encode(i18n_r('MENU_TEXT')); ?> + '</label></span>');
      $('#post-menu-order').attr('style','clear:both;').addClass('short');
      $('#post-menu-order').before('<span style="text-transform:none;float:left;"><label for="post-menu">' + <?php echo json_encode(i18n_r('i18n_navigation/INSERT_AFTER')); ?> + '</label></span>');
    } else if ($('div.leftopt').length > 0) { // GetSimple 3.0
      $('label[for=post-menu-enable]').nextUntil('input').remove();
      $('#menu-items').css('height','auto');
      $('#post-menu').attr('style','').addClass('short');
      $('#post-menu').prev().remove();
      $('#post-menu').prev().remove();
      $('#post-menu').prev().remove();
      $('#post-menu').before('<label for="post-menu">' + <?php echo json_encode(i18n_r('MENU_TEXT')); ?> + '</label>');
      $('#post-menu-order').attr('style','').addClass('short');
      $('#post-menu-order').before('<label for="post-menu">' + <?php echo json_encode(i18n_r('i18n_navigation/INSERT_AFTER')); ?> + '</label>');
    } else {
      $a = $('#post-menu-enable').closest('td').find('a');
      $a.after($a.html());
      $a.remove();
      $('#post-menu').closest('div').find('span').empty().html(<?php echo json_encode(i18n_r('MENU_TEXT')); ?>);
      $('#post-menu').attr('style','').after(<?php echo json_encode('<br /><span>'.i18n_r('i18n_navigation/INSERT_AFTER').'</span>'); ?>);
      $('#post-menu-order').attr('style','').before('<br />');
    }  
    $('#post-parent').change(changeParent);
    $('#post-id').change(changeUrl);
    changeParent();
    changeUrl();
    $('#post-metak').autocomplete(tags, {
      minChars: 0,
      max: 50,
      scroll: true,
      multiple: true,
      multipleSeparator: ', '
    });
  });
  // ]]>
</script>

This is what I suspected as the main error reason after investigation:

if ($('#tick').size() > 0) {

But replacing it with length didn't resolve the issue.

jQuery version: jquery-3.2.1.min.js?v=3.2.1

like image 459
David Buik Avatar asked Jan 28 '19 23:01

David Buik


2 Answers

Do not use $(...).size(), it is deprecated as of jQuery 1.8. Use the .length property instead.

For more information see .size() | jQuery API Documentation


After replacing size() with length, it turned out that the actual problem is reference to non-existnent element. This implies that undefined is indeed not a function.

like image 77
Mysak0CZ Avatar answered Oct 11 '22 11:10

Mysak0CZ


size() function has been deprecated. add below code:

$(function() {
    $.fn.size = function() {
        return this.length;
    }
});

You don't need to change code.

like image 42
Jitendra Rajput Avatar answered Oct 11 '22 10:10

Jitendra Rajput