Title: Uncaught TypeError
Last modified: January 30, 2017

---

# Uncaught TypeError

 *  [RebellionNT1](https://wordpress.org/support/users/rebellionnt1/)
 * (@rebellionnt1)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/)
 * Hi Guys,
 * When loading my page the only error I have is the following:
 * Uncaught TypeError: $ is not a function
    at autoptimize_7d81ce8….php:4369 (anonymous)
   @ autoptimize_7d81ce8….php:4369
 * The affected line is the following:
 *     ```
       ;$(function($){"use strict";function insertParam(key,value){key=encodeURI(key);value=encodeURI(value);var kvp=document.location.search.substr(1).split('&');var i=kvp.length;var x;while(i--){x=kvp[i].split('=');if(x[0]==key){x[1]=value;kvp[i]=x.join('=');break;}}
       ```
   
 * I do not know what this error is, any solution?

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8714875)
 * well, in wordpress one is not supposed to use `$` just like that, but to use 
   the no-conflict `jQuery` instead, cfr. [https://developer.wordpress.org/reference/functions/wp_enqueue_script/#comment-1473](https://developer.wordpress.org/reference/functions/wp_enqueue_script/#comment-1473)
 * so ideally you should find the culprit code and (have someone) fix that. else
   you might be able to work around it by excluding some JS from optimization, but
   can’t tell you which one from where I’m sitting I’m afraid … 😉
 *  Thread Starter [RebellionNT1](https://wordpress.org/support/users/rebellionnt1/)
 * (@rebellionnt1)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715047)
 * I have detected that the file js/script.js of my theme, Has this code:
 *     ```
       $(function($) {
   
           "use strict";
   
           function insertParam(key, value) {
               key = encodeURI(key);
               value = encodeURI(value);
   
               var kvp = document.location.search.substr(1).split('&');
   
               var i = kvp.length;
               var x;
               while (i--) {
                   x = kvp[i].split('=');
   
                   if (x[0] == key) {
                       x[1] = value;
                       kvp[i] = x.join('=');
                       break;
                   }
               }
   
               if (i < 0) {
                   kvp[kvp.length] = [key, value].join('=');
               }
   
               //this will reload the page, it's likely better to store this until finished
               document.location.search = kvp.join('&');
           }
       ```
   
 * How can I modify it so it does not show autoptimize errors?.
 * Best Regards
 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715070)
 * As per the info in the link above, wrap the entire block in e.g.;
 *     ```
       (function( $ ) {
           // add your $-dependant javascript code here 
       })(jQuery);
       ```
   
 *  Thread Starter [RebellionNT1](https://wordpress.org/support/users/rebellionnt1/)
 * (@rebellionnt1)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715099)
 * I put it this way and it still gives the error: Is correct?
 *     ```
       $(function($) {
   
           "use strict";
   
           function insertParam(key, value) {
               key = encodeURI(key);
               value = encodeURI(value);
   
               var kvp = document.location.search.substr(1).split('&');
   
               var i = kvp.length;
               var x;
               while (i--) {
                   x = kvp[i].split('=');
   
                   if (x[0] == key) {
                       x[1] = value;
                       kvp[i] = x.join('=');
                       break;
                   }
               }
   
               if (i < 0) {
                   kvp[kvp.length] = [key, value].join('=');
               }
   
               //this will reload the page, it's likely better to store this until finished
               document.location.search = kvp.join('&');
           }(jQuery);
       ```
   
 * Best regards
 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715136)
 * no, that’s no correct, try this instead;
 *     ```
       (function($) {
           $(function($) {
               "use strict";
   
               function insertParam(key, value) {
                   key = encodeURI(key);
                   value = encodeURI(value);
   
                   var kvp = document.location.search.substr(1).split('&');
   
                   var i = kvp.length;
                   var x;
                   while (i--) {
                       x = kvp[i].split('=');
   
                       if (x[0] == key) {
                           x[1] = value;
                           kvp[i] = x.join('=');
                           break;
                       }
                   }
   
                   if (i < 0) {
                       kvp[kvp.length] = [key, value].join('=');
                   }
   
                   //this will reload the page, it's likely better to store this until finished
                   document.location.search = kvp.join('&');
               }
           }
       }(jQuery);
       ```
   
 *  Thread Starter [RebellionNT1](https://wordpress.org/support/users/rebellionnt1/)
 * (@rebellionnt1)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715163)
 * If I try with that code, the slider of my web does not work, any ideas?
 * Best regards.
 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715166)
 * it’s very much possible that the error we’re discussing here is blocking the 
   slider 🙂
 *  Thread Starter [RebellionNT1](https://wordpress.org/support/users/rebellionnt1/)
 * (@rebellionnt1)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715198)
 * As I can give you more information to try to solve the problem?, with the original
   code the slider works correctly, but with its correction fails.
 * Best Regards
 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715474)
 * ah, I misunderstood 🙂
 * In that case I would roll-back the change, no?
 *  Thread Starter [RebellionNT1](https://wordpress.org/support/users/rebellionnt1/)
 * (@rebellionnt1)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715521)
 * I have left the file as it originally came, because with your change the slider
   does not work for me.
 * Best Regards.
 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715555)
 * that seems like a sensible thing to do. to fix the original error, i would get
   in touch with the theme-developer and ask him/ her to adapt it so it doesn’t 
   depend on the pre-existance of $ any more.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Uncaught TypeError’ is closed to new replies.

 * ![](https://ps.w.org/autoptimize/assets/icon-256X256.png?rev=2211608)
 * [Autoptimize](https://wordpress.org/plugins/autoptimize/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/autoptimize/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/autoptimize/)
 * [Active Topics](https://wordpress.org/support/plugin/autoptimize/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/autoptimize/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/autoptimize/reviews/)

## Tags

 * [uncaught TypeError](https://wordpress.org/support/topic-tag/uncaught-typeerror/)

 * 11 replies
 * 2 participants
 * Last reply from: [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * Last activity: [9 years, 4 months ago](https://wordpress.org/support/topic/uncaught-typeerror-16/#post-8715555)
 * Status: not resolved