Uncatch TypeError: unDefinition 不是一个加载 jquery-min. js 的函数

我正在建立一个正常的网页,需要我加载大约5个 CSS 文件和10个 Javascript 文件。

  • 当在 HTML 页面中分别加载它们时,我的网页加载得很好。
  • 现在开始生产,我按照需要的顺序将所有 Javascript 连接到一个文件中,并将所有 CSS 连接到另一个文件中。但当我试图运行连接文件的网页时,它会抛出一个错误:

    Uncaught TypeError: undefined is not a function

在连接的 Javascript 文件中加载 jquery.min.js 的行上。

我能做什么来减轻这个? 我想连接所有的文件,并缩小他们的生产。请帮助。


编辑: 我将 Javascript 和 CSS 按照它们单独加载时的顺序进行了合并,并且运行良好。

452768 次浏览

You might have to re-check the order in which you are merging the files, it should be something like:

  1. jquery.min.js
  2. jquery-ui.js
  3. any third party plugins you loading
  4. your custom JS

Assuming this problem still has not be resolved, a lot of individual files don't end their code with a semicolon. Most jQuery scripts end with (jQuery) and you need to have (jQuery);.

As separate files the script will load just fine but as one individual file you need the semicolons.

I had this problem recently with the jQuery Validation plug-in, using Squishit, also getting the js error:

"undefined is not a function"

I fixed it by changing the reference to the unminified jquery.validate.js file, rather than jquery.validate.min.js.

@MvcHtmlString.Create(
@SquishIt.Framework.Bundle.JavaScript()
.Add("~/Scripts/Libraries/jquery-1.8.2.min.js")
.Add("~/Scripts/Libraries/jquery-ui-1.9.1.custom.min.js")
.Add("~/Scripts/Libraries/jquery.unobtrusive-ajax.min.js")
.Add("~/Scripts/Libraries/jquery.validate.js")
.Add("~/Scripts/Libraries/jquery.validate.unobtrusive.js")
... more files

I think that the minified version of certain files, when further compressed using Squishit, for example, might in some cases not deal with missing semi-colons and the like, as @Dustin suggests, so you might have to experiment with which files you can doubly compress, and which you just leave to Squishit or whatever you're bundling with.

I just had the same message with the following code (in IcedCoffeeScript):

f = (err,cb) ->
cb null, true


await f defer err, res
console.log err if err

This seemed to me like regular ICS code. I unfolded the await-defer construct to regular CoffeeScript:

f (err,res) ->
console.log err if err

What really happend was that I tried to pass 1 callback function( with 2 parameters ) to function f expecting two parameters, effectively not setting cb inside f, which the compiler correctly reported as undefined is not a function.

The mistake happened because I blindly pasted callback-style boilerplate code. f doesn't need an err parameter passed into it, thus should simply be:

f = (cb) ->
cb null, true
f (err,res) ->
console.log err if err

In the general case, I'd recommend to double-check function signatures and invocations for matching arities. The call-stack in the error message should be able to provide helpful hints.

In your special case, I recommend looking for function definitions appearing twice in the merged file, with different signatures, or assignments to global variables holding functions.

I've run into the very same issue, when mistakenly named variable with the very same name, as function.

So this:

isLive = isLive(data);

failed, generating OP's mentioned error message.

Fix to this was as simple as changing above line to:

isItALive = isLive(data);

I don't know, how much does it helps in this situation, but I decided to put this answer for others looking for a solution for similar problems.

Yes, i also I fixed it changing in the js libraries to the unminified.

For example, in the tag, change:

<script type="text/javascript" src="js/jquery.ui.core.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.widget.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.rcarousel.min.js"></script>

For:

<script type="text/javascript" src="js/jquery.ui.core.js"></script>
<script type="text/javascript" src="js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="js/jquery.ui.rcarousel.js"></script>

Quiting the 'min' as unminified.

Thanks for the idea.

I got the same error from having two references to different versions of jQuery.

In my master page:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

And also on the page:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script>

Make sure you have commented out any commentaries. Sometimes when copying and pasting you will leave out the "/*!"

Also when you go into the console they will list your errors and you should take it one at a time. If you see "Uncaught SyntaxError: Unexpected token * " That might mean it is reading your js file and it isn't getting past the first line.

* jquery.tools 1.1.2 - The missing UI library for the Web * * [tools.tabs-1.0.4, tools.tooltip-1.1.2, tools.scrollable-1.1.2, tools.overlay-1.1.2, tools.expose-1.0.5] * * Copyright (c) 2009 Tero Piirainen * http://flowplayer.org/tools/ * File generated: Wed Oct 07 09:40:16 GMT 2009 */

For those out there who still couldn't fix this, I did so by changing my 'this' to '$(this)' when using jQuery.

E.G:

$('.icon').click(function() {
this.fadeOut();
});

Fixed:

$('.icon').click(function() {
$(this).fadeOut();
});

In case there are any morons out there like me, I had this frustrating problem because I forgot a simple

new

keyword before instantiating a new object.

This solution worked for me



;(function($){
// your code
})(jQuery);


Move your code inside the closure and use $ instead of jQuery

I found the above solution in https://magento.stackexchange.com/questions/33348/uncaught-typeerror-undefined-is-not-a-function-when-using-a-jquery-plugin-in-ma

after seraching too much

Remember: Javascript functions are CASE SENSITIVE.

I had a case where I'm pretty sure that my code would run smoothly. But still, got an error and I checked the Javascript console of Google Chrome to check what it is.

My error line is

opt.SetAttribute("value",values[a]);

And got the same error message:

Uncaught TypeError: undefined is not a function

Nothing seems wrong with the code above but it was not running. I troubleshoot for almost an hour and then compared it with my other running code. My error is that it was set to SetAttribute, which should be setAttribute.

I got this when I accidentally passed too many parameters into a jquery function that only expected one callback parameter.

For others troubleshooting: make sure you check all your jquery function calls for extra parameters.