How to solve object required error when calling java script from script folder?

Hi

I have developed a website with a master page which has menus to navigate to different pages. I have written the following code to hide and show the menus.  It works fine on my developer machine but I get "object required" error message on the production server.
I call the js code from the script folder in ASP.NET 4 website.

Can you please help?

Thanks

Jose

]This is called from the maste page:

 <!--BEGIN TO NAVIGATE-->  
   <script type="text/javascript" src='<%= Page.ResolveUrl("/Scripts/JoeNavigation.js") %>'>/</script>    
 <!--END TO NAVIGATE-->


This list is for Menus:

<div id="menu">
            <ul>
<li><a href="<%=Request.ApplicationPath%>/Default.aspx" accesskey="1" title="" id="Home">Home</a></li>
<li><a href="<%=Request.ApplicationPath%>/QuickQuery.aspx" accesskey="2" title="" id="QuickQuery">Quick Query</a></li>
                  <li><a href="" accesskey="3" title="" id="GRRAdmin">GRR Admin</a></li>      
<li><a href="<%=Request.ApplicationPath%>/About.aspx" accesskey="4" title="" id="About">About</a></li>
<li><a href="<%=Request.ApplicationPath%>/Help.aspx" accesskey="5" title="" id="Help">Help</a></li>       
                  <li><a href="" accesskey="6" title="" id="UserLoginDisplay">Login</a></li>  
<li><a href="<%=Request.ApplicationPath%>/ChangePassword.aspx" accesskey="7" title="" id="ResetPassword">Reset Password</a></li>                          
</div>



This Java script code is saved with  .js extention in "script" folder:

//BEGIN MENU NAVIGATIONS

    function HideMyAdmin() {
         document.getElementById("MyAdmin").style.display = "none";
         document.getElementById("ResetPassword").style.display = "none";    
    }

    function HideMyAdminAndLogin() {
        document.getElementById("MyAdmin").style.display = "none";
        document.getElementById("UserLoginDisplay").style.display = "none";
        document.getElementById("ResetPassword").style.display;      
    }

    function ShowMyAdmin() {
        document.getElementById("UserLoginDisplay").style.display = "none";
        document.getElementById("ResetPassword").style.display      
    }

    function HideALL() {
        document.getElementById("MyQuery").style.display = "none";
        document.getElementById("MyAdmin").style.display = "none";
        document.getElementById("ResetPassword").style.display = "none";    
    }

    function HideLogin() {      
        document.getElementById("UserLoginDisplay").style.display = "none";
    }

    function HideForChangePw() {
        document.getElementById("MyAdmin").style.display = "none";
        document.getElementById("UserLoginDisplay").style.display = "none";
    }
     
    //END
JOLEEJJAsked:
Who is Participating?
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
It works fine on my developer machine but I get "object required" error message on the production server.

Some of the solutions (can sound absurd but it's the technology) that I'm able to get out for this problem are:

S.1. The code part:

<script type="text/javascript" src='<%= Page.ResolveUrl("/Scripts/JoeNavigation.js") %>'></script> 

Open in new window


displays in browser's rendered source code as: <script type="text/javascript" src="Scripts/JoeNavigation.js">/</script>

Since we have hosted our website in some website or virtual directory on hosting server, the above path would not be correct in that context.

For that modify the code as:

 <script type="text/javascript" src="<%= Page.ResolveUrl("~")%>Scripts/JoeNavigation.js"></script>

Open in new window


So what's the difference?
Page.ResolveUrl("~") would return root directory of your website. And path would be rendered as:

<script type="text/javascript" src="WebsiteName/Scripts/JoeNavigation.js">/</script>

And I believe that alert function (as I asked for testing) would also work this time.

S.2. I'm not so much well versed with Javascript. But I found some difference of functions working.  Discussed below:

You used function from code-behind file as:

Page.ClientScript.RegisterStartupScript(Me.GetType(), joescript, "HideMyAdmin()", True)

I tested it and it didn't work fine though script loaded  successfully. So modified the function with adding semicolon (;) as suffix as:

Page.ClientScript.RegisterStartupScript(Me.GetType(), joescript, "HideMyAdmin();", True)

And it worked.

S.3. There is an interesting point to take care of while working with Telerik and custom javascript.

All or most of the telerik controls render their js files for making telerik work on client side. Since these files would increase on basis of telerik controls. And this would make page's response slow. To optimize such things there is control named RadScriptManager.

And I assume that you might be using same. RadScriptManager combines all of the controls rendered scripts into single script using HttpHandler. And this sometimes make custom javascript to not to work as expected.

So for that scenario you can use following line of code:

Telerik.Web.UI.RadScriptManager.RegisterStartupScript(this, typeof(Page), joescript, "HideMyAdmin();", true);

Open in new window

0
 
MrunalCommented:
Hi Jose,
We can not trace your issue.
Better way, you can solve this yourself.
Open same URL in Mozilla Firefox. Open Firebug add on for Firefox. and just refresh page once.

In Firebug window, in Console tab, you will get exact error, what is missing and which object is not found.
Hope this helps you.
0
 
Obadiah ChristopherCommented:
1. What line are you getting the error?

2. <script type="text/javascript" src='<%= Page.ResolveUrl("/Scripts/JoeNavigation.js") %>'>/</script>

What does this line render in the actual page?

3. <a href="<%=Request.ApplicationPath%>/Default.aspx" accesskey="1" title="" id="Home">Home</a>

What does this render?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Lines from your question part:

1. I call the js code from the script folder in ASP.NET 4 website.

2. script type="text/javascript" src='<%= Page.ResolveUrl("/Scripts/JoeNavigation.js") %>'>/</script>    

This Java script code is saved with  .js extention in "script" folder

So let us know is the javascript file is in script folder? If yes change the folder name to Scripts because the same javascript file is not accessible as per 2nd point.

Share back whether it helped or not.
0
 
JOLEEJJAuthor Commented:
Hi Informaniac,

1)  The error is : "ReferenceError: HideGRRAdmin is not defined"
2)  This code renders in Login.aspx page (which uses the master page)
3)  Actually, the default page is not the loading page but the login.aspx page.

Thanks

Joe
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
The error is : "ReferenceError: HideGRRAdmin is not defined"

I think you are calling js function HideGRRAdmin() from some part of your page. But it has been not defined in your javascript file.

Could you please ensure the same?
0
 
JOLEEJJAuthor Commented:
Hi

I am sorry the error is:  " ReferenceError: HideMyAdmin is not defined"

Following is one of the functions that is written in js script called "JoeNavigation.js" which is located on the Script folder as mentioned in earlier post.

  function HideMyAdmin() {
         document.getElementById("MyAdmin").style.display = "none";
         document.getElementById("ResetPassword").style.display = "none";    
    }
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
OK. To test with if the javascript file is accessible and working fine or not. Could you please write line in top of js file as:

alert("Called from JoeNavigation.js file");

Do you able to see the above message while running your application?
0
 
JOLEEJJAuthor Commented:
Hi,

No. I couldn't get the alert message. It is weired.
I  get the same error message:
 "ReferenceError: HideMyAdmin is not defined"
0
 
JOLEEJJAuthor Commented:
Hi,

The function "HideMyAdmin" is called on the page load event of Login.aspx page.

'== begin to hide Myadmin menu
        Dim joescript As String = "joescript"
        Page.ClientScript.RegisterStartupScript(Me.GetType(), joescript, "HideMyAdmin()", True)
        '== end to hide Myadmin menu
0
 
JOLEEJJAuthor Commented:
Hi,

I don't even get the alert message on my desktop but the "HideMyAdmin()" function still works fine on my desktop but not on the production server  It is too weird for me now...
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
We would try to get solution for same. But before that could you please answer few questions:

Q.1. In which folder (script or scripts) the JoeNavigation.js script file is?

Q.2. See your page's source code from browser and get the following line's output:

<!--BEGIN TO NAVIGATE-->  
   <script type="text/javascript" src='<%= Page.ResolveUrl("/Scripts/JoeNavigation.js") %>'>/</script>    
 <!--END TO NAVIGATE-->

I still believe that src line's display would be different from the one displayed on your desktop.
0
 
Albert Van HalenAnalyst developerCommented:
Check if the js file is sent to your browser from your webserver.
You can check in your browser by typing in the location of the js-file in the address bar.

NB: If you use IIS7, make sure that static content is being handled.
Check via Control Panel -> Programs & Features -> Turn Windows features on or off -> Internet Information Services -> World Wide Web Server -> Common HTTP Features.
Make sure Static Content is checked.
0
 
mcnuteCommented:
include a document ready function and wrap it around your hole JoeNavigation.js script like this:

if (document.readyState === "complete") { /*joeNavigation function here...*/ }

Open in new window

0
 
JOLEEJJAuthor Commented:
Hi,

I tried the document ready function and it is still the same problem. I browed the file from the address bar, it brings the file without any problem. The path of the file is the same as in view source as well.

 Following is the error message from my localhost.

Please have a look,

Thanks

Joe


Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0E; InfoPath.3; Zune 4.7; .NET4.0C)
Timestamp: Thu, 30 Aug 2012 23:57:18 UTC


Message: Object expected
Line: 384
Char: 1
Code: 0
URI: http://localhost/MyWebvNET/Login.aspx
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
If possible could you share your Login.aspx, Master page and javascript file for testing the problem. As it is hard to debug problem without actually looking all details.

Again if it is confidential please do not share.
0
 
JOLEEJJAuthor Commented:
Hi,

I am very sorry, I am not in a position to share the pages.

Thanks

Joe
0
 
mcnuteCommented:
Maybe you didn't understand quite well. I meant to modify the JoeNavigation.js, so it's functions are wrapped into the document ready if clause. Did you do that?
0
 
Albert Van HalenAnalyst developerCommented:
Ok, so there must be an error in your js-file.

According to tour post, the ShowMyAdmin method has an invalid statement.
function ShowMyAdmin
   document.getElementById("UserLoginDisplay").style.display = "none";
   document.getElementById("ResetPassword").style.display
}

Open in new window

The semi-colon on the second line is missing.
It's a useless statement anyway, you probably want to set it to"none"?
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
1. Could you modify this line:

<script type="text/javascript" src='<%= Page.ResolveUrl("/Scripts/JoeNavigation.js") %>'>/</script>   

Open in new window


by

<script type="text/javascript" src='<%= Page.ResolveClientUrl("/Scripts/JoeNavigation.js") %>'>/</script>   

Open in new window


I know both work same way. But can you give it try.

2. Are you using ajax in your application?
0
 
JOLEEJJAuthor Commented:
Hi

I have set document.getElementById("ResetPassword").style.display = "none" and changed
Page.ResolveUrl to Page.ResolveClientUrl. I still get the same error message.
I did wrap the JoeNavigation.js function with document.readyState == "complete".

Yes. I am using Telerik asp.net ajax controls in my application.

Thanks

Joe
0
 
JOLEEJJAuthor Commented:
All the functions in JoeNavigation.js works fine if they are put in master page even on production server. But it doesn't like to be  called from the Script folder.

Thanks

Joe
0
 
JOLEEJJAuthor Commented:
Hi vs00saini

That Ellipsis made the magic. It works fine now.

 <script type="text/javascript" src="<%= Page.ResolveUrl("~")%>Scripts/JoeNavigation.js"></script>

You are a Star,

Thanks for time and help,

Cheers,

Joe
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi Joe,

I am double happy as all efforts went towards fruitful success. Happy coding :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.