Link to home
Start Free TrialLog in
Avatar of Joseph Jones
Joseph JonesFlag for Australia

asked on

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
Avatar of Mrunal
Mrunal
Flag of India image

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.
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?
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.
Avatar of Joseph Jones

ASKER

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
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?
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";    
    }
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?
Hi,

No. I couldn't get the alert message. It is weired.
I  get the same error message:
 "ReferenceError: HideMyAdmin is not defined"
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
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...
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.
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.
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

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
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.
Hi,

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

Thanks

Joe
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?
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"?
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?
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
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
ASKER CERTIFIED SOLUTION
Avatar of Vikram Singh Saini
Vikram Singh Saini
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Hi Joe,

I am double happy as all efforts went towards fruitful success. Happy coding :)