"Error: $.cookie is not a function" after converting to Web Application project

Topics: ASP.NET 2.0, Controls
Developer
Jan 25, 2011 at 1:43 PM

I've converted BE 2.0 to Web Application project and now I'm getting "$.cookie is not a function" error when trying to load any list in admin panel (posts, comments, etc..)

Does anyone knows what might be the problem? Path to a plugin seems to be fine. I have no idea where to look next.

Jan 25, 2011 at 1:46 PM

I would expect that the scripts form 'lib' subdirectory do not belong to the project.

Developer
Jan 25, 2011 at 1:53 PM

Seems as if Cookie plugin that is used here is stored in /scripts/ folder in Web app project and it's loaded each time properly.

Jan 26, 2011 at 12:27 AM
dzovan wrote:

Seems as if Cookie plugin that is used here is stored in /scripts/ folder in Web app project and it's loaded each time properly.

I think, you're right., $.cookie is defined in jQuery.Cookie.js

Can you give more information like a line number where the error occurred or StackTrace?

Developer
Jan 26, 2011 at 11:10 AM

When you try to load posts, for instance, you would get this:

Error: $.cookie is not a function
Source File: http://localhost:50128/js.axd?path=%2fadmin%2fadmin.js
Line: 634

And that line is in the function:

function LoadPostsForPage(page) {
if(page == null || page == 0)
page = 1;

$.cookie('postCurrentPage', page, { expires: 7 }); <- line 634
LoadPosts();
}

All scripts seem to be loaded successfully, so I have no idea what it might be.

Jan 26, 2011 at 3:29 PM

What browser do you use and what Javascript diagnostic settings?

Developer
Jan 26, 2011 at 4:35 PM

It's Firefox 3.6.13 and Firebug. Google chrome dev tool report this error for the same line:

  1. Uncaught TypeError: Object function (a,b){return new c.fn.init(a,b)} has no method 'cookie'
    1. LoadPostsForPage
    2. (anonymous function)

Thanks!

  1. Uncaught TypeError: Object function (a,b){return new c.fn.init(a,b)} has no method 'cookie'
    1. LoadPostsForPagejs.axd:634
    2. (anonymous function)
Jan 26, 2011 at 4:46 PM
Edited Jan 26, 2011 at 5:02 PM

It executes correctly for me. i don't see this error in Google Chrome dev Tools.

 

Jan 26, 2011 at 10:02 PM

If you view page source, the page header should contain <script type="text/javascript" src="/BlogEngine.NET/js.axd?path=Scripts%2fjquery.cookie.js"></script> along with other script references.

It's inserted by JavaScriptHandler.

Developer
Jan 26, 2011 at 10:08 PM

Checked, it's there. That's what makes it strange...

Coordinator
Jan 27, 2011 at 8:11 AM

One thing to look out for .... is see if the main jQuery script is being output on the page twice.  For example, consider this:

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

In this case, after these 3 script tags execute, $.cookie is not defined.  This is because when the 2nd jQuery script is loaded, it clears out the jQuery object from the 1st jQuery script along with any functions/plug-ins that were added (e.g. cookie) to the 1st instance of the jQuery object.

So I guess what to check for is (a) which scripts are on the page, and (b) the order of the scripts, and (c) if a script is being output on the page more than once.

I'm not saying this is necessarily the cause of your situation, but just one scenario where this type of error could happen.

 

Developer
Jan 27, 2011 at 10:59 AM

Thank you Ben!! The order is the problem I guess - cookie plugin is referenced before jQuery library! This is the order of scripts:

<script type="text/javascript" src="/js.axd?path=Scripts%2fjquery.js"></script>
<script type="text/javascript" src="/js.axd?path=Scripts%2fjquery.cookie.js"></script>
<script type="text/javascript" src="/js.axd?path=Scripts%2fjquery.validate.min.js"></script>
<script type="text/javascript" src="/js.axd?path=Scripts%2fjquery-1.4.1.js"></script>
<script type="text/javascript" src="/js.axd?path=Scripts%2fjquery-1.4.1.min.js"></script>
<script type="text/javascript" src="/js.axd?path=Scripts%2fjquery-jtemplates.js"></script>
<script type="text/javascript" src="/js.axd?path=Scripts%2fjson2.js"></script>
<script type="text/javascript" src="/js.axd?path=%2fadmin%2fadmin.js"></script>

Is there a way to set Javascriphandler to load scripts in some order?

Coordinator
Jan 27, 2011 at 7:00 PM

The top script tag you have there "jquery.js" *is* the jQuery library.  In BE 2.0, I think "jQuery.js" represents jQuery version 1.4.4.

You actually don't need the other 2 jQuery scripts .... jquery-1.4.1.js and jquery-1.4.1.min.js.  Those 2 can be removed.

It might be that those other 2 jQuery scripts are in your /Scripts folder.  At least on the front end of the site, all the scripts in the /Scripts folder are automatically added to the page.  So if you find those 2 JS files in the /Scripts folder, then I would just delete them.

If they are not in the /Scripts folder, then they are coming from somewhere else ... you could search the project for "jquery-1.4.1.js" to see where they might be coming from.

Developer
Jan 31, 2011 at 9:08 AM

That solved the issue, thanks Ben!

Jan 31, 2011 at 12:39 PM

dzovan,

I've done my own BE 2.0 WA and I was wondering:

- how did you approach the problem of seeing extensions code from the admin page?

- I don´t know how to make source only extensions work without building them against BE.

- Where did you move the code inside the old App_Code. Using Visual Studio wizard I ended up with a Old_App_Code folder ... ugly. Surely those classes belong to a place with a better name but I know little or nothing about BE.

 

Thanks for any ideas.

Federico

Jan 31, 2011 at 8:30 PM
fdelgad wrote:

- Where did you move the code inside the old App_Code. Using Visual Studio wizard I ended up with a Old_App_Code folder ... ugly. Surely those classes belong to a place with a better name but I know little or nothing about BE.

 

Classes in App_code are server controls. If you want to move them, move them into a separate assembly (control library)

 

 

Developer
Feb 5, 2011 at 12:53 PM

fdelgad,

I'm not the right person for backend dev things, but I'll try to explain it. In Utils.cs file that is in BlogEngine.Code project, you have this method:

public static IEnumerable<Assembly> CodeAssemblies() { ... }

I found, once, somewhere (I don't remember where, maybe it was on codeplex too) some pieces of code that I played with, and I came up with this:

                if (s != null && s.CodeSubDirectories != null && s.CodeSubDirectories.Count > 0)
                {
                    for (var i = 0; i < s.CodeSubDirectories.Count; i++)
                    {
                        assemblyName = string.Format("App_SubCode_{0}", s.CodeSubDirectories[i].DirectoryName);
                        codeAssemblies.Add(Assembly.Load(assemblyName));
                    }
                }
                else
                {
                    if (!IsMono)
                    {
                        assemblyName = "App_Code";
                    }

                    //codeAssemblies.Add(Assembly.Load(assemblyName)); Comment this line
                }
                // Add the line below
                codeAssemblies.Add(Assembly.LoadFile(HttpContext.Current.Server.MapPath("~/bin/BlogEngine.Web.dll")));

                codeAssemblies.AddRange(GetCompiledExtensions());

I guess it loads the proper assembly, but honestly I've no idea how :) This makes extensions to work.

With regards to App_Code, I left it as Old_App_Code.. it doesn't matter much to me.

Feb 6, 2011 at 3:11 PM

Thanks!

 

I will give it a try. I just need to fix the "View Source" little issue and then move on to the 100% Mono-fied build.