Tuesday, January 25, 2011

JavaScript Error - Object Not Found

Problem:
When you refer JavaScript/css/images files, it will be referred properly and the code works fine in local machine. But you may get JavaScript error "Object Not Found" when deployed in web servers.


Solution:
Kindly use the  symbol to avoid this Object Not Found error to specify the root directory.


Sample Code:
The below code which generates "Object Not Found" JavaScript error,
<link href="/Content/Styles/MasterPage.css" rel="stylesheet" type="text/css" />
Or 
<link href="../Content/Styles/MasterPage.css" rel="stylesheet" type="text/css" />


Working Code:
Use  symbol before the / to refer the root level as shown below,
<link href="~/Content/Styles/MasterPage.css" rel="stylesheet" type="text/css" />
In addition, if you are working in MVC, use Url.Content to get absolute URL as shown below, 
<script src="<%= Url.Content("~/Scripts/jquery-1.4.4.js") %>" type="text/javascript">script>


Conclusion
Make a practice of referring any external files by using the ~ symbol in the root level to avoid Object Not Fund error.


Happy Coding


No comments:

Post a Comment