Geeks With Blogs


Microsoft MVP


Moderator at CodeASP.NET

Quiz Master

free counters
Free counters
Added on January 19,2012

Follow Me @vmsdurano

A bit About Me

The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.

Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

I was working with a project that uses the concept of master page. Within the master page I have a bunch of UserControls for some specific purposes and one of the UserControl is for the site dynamic menu. The menu was basically built using JQuery and CSS. The menu works fine if you are using it as an anonymous user but if you logged in as authorize user the menu will no longer work for some odd reasons and produces the following script error below in the browser:

Message: Object expected
Line: 122
Char: 5
Code: 0
URI: http://domain/page/auth/home.aspx

Here's how I referenced the JQuery script within the UserControl:

<script src="../scripts/jquery_4.1.js" type="text/javascript"></script>
<script type="text/javascript">
   $(document).ready(function() {
       //do stuffs here

Again, the code above works only for public pages but not for secured pages after you logged in as valid user. After digging through the codes and after doing a thorough investigation, I found out that the "Object Expected" error occurs when you try to access an object which is not defined, not referenced or mistakenly misspelled. First I'm guessing that when navigating to the secured pages the previous script reference fails to locate the actual path of the JQuery script that's why in error above, Line 122 refers the $(document).ready(function().

Unfortunately, I cannot use tilde (~) since it doesn’t work for standard html elements, such as images, css and JavaScript. So I tried using the following code below but to no avail. It will still produces the same error when navigating through secured pages.

<script src="../../scripts/jquery_4.1.js" type="text/javascript"></script>

The Solution

Luckily, ASP.NET's Control class provides the handy ResolveUrl method that lets you easily use and parse relative URLs. ResolveUrl fixes up urls that start with ~ and puts the applications relative root path in its place. So to fix the issue you can use ResolvedURL for referencing the path like:

<script src='<%= Page.ResolveUrl("~/scripts/jquery_4.1.js")%>' type="text/javascript"></script>

That's it! I hope you'll find this post useful!

Posted on Wednesday, October 6, 2010 7:44 PM ASP.NET , Tips&Tricks | Back to top

Comments on this post: Master Page and JQuery: Solving Object Expected Error

# re: Master Page and JQuery: Solving Object Expected Error
Requesting Gravatar...
uber helpful - many thanks for his.
Left by will on Oct 23, 2010 4:42 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: