mjroodt
mjroodt

Reputation: 3313

javascript object expexted

I am unable to call a javascript function as it says "Object Expected". I am using asp.net and this is being used on a standard aspx page with some usercontrols. One of the user controls called CookieControl calls a javascript method setandCheckCookie() on document.ready.i.e

$(document).ready(function() {
         setandCheckCookie();
)};

In the head section on my aspx page I have reference the javascript file before I add the control:

<script src="CookiesControl/js/Cookie.js" type="text/javascript"></script>
<uc2:Cookie ID="Cookie1" runat="server" />

I've event tried adding the script reference on the user control itself but when I go to my aspx page I get the object expected error.

I don't believe its the control or the js file as both these are used elsewhere in the website and they work fine but something I'm doing is stopping the js file from being reference because in firebug and IE debugger I can't see the referenced js file.

Perhaps the path is incorrect but I have dragged the script from the solution explorer and visual studio isn't complaining about the path either.

So my question is: what could potentially stop my javascript file from being referenced.

Upvotes: 0

Views: 112

Answers (1)

Amiram Korach
Amiram Korach

Reputation: 13296

Visual Studio complaining or not about a path is not always saying what you'll get in the browser. To check if the script is referenced ok, try to get the script file in the browser. For example, if you page is at http://yoursite/yourpage.aspx and your script reference in the html code (by View Source, not what you see in Visual Studio) is folder/scriptFile.js, put this address http://yoursite/folder/scriptFile.js and see if you get the file. If not, then it is not referenced ok. To solve issues like that, put the full path after yoursite, like:

<script src="/ScriptVirtualFolder/CookiesControl/js/Cookie.js" type="text/javascript"></script>

where ScriptVirtualFolder is in your site root folder, and it is a virtual folder, means it is accessible by the browser in this path. Also, you can use asp.net to resolve your path by using the ~ sign and runat='server'. For example:

<script src="~/ScriptServerFolder/CookiesControl/js/Cookie.js" type="text/javascript" runat="server"></script>

Upvotes: 1

Related Questions