views:

350

answers:

1

I'm semi-new to writing ASP.Net applications using master pages and I've run into an issue I've spent some time on but can't seem to solve.

My situation is that I have a master page with a structure that looks like this:

<head runat="server"> 
    <title>Test Site</title>   
    <link rel="Stylesheet" type="text/css" href="Default.css" />
    <script type="text/javascript" language="javascript" src="js/Default.js" />
    <meta http-equiv="Expires" content="0"/>
    <meta http-equiv="Cache-Control" content="no-cache"/>
    <meta http-equiv="Pragma" content="no-cache"/>        
    <asp:ContentPlaceHolder ID="cphHead" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">       

        <div id="divHeader">
        <asp:ContentPlaceHolder ID="cphPageTitle" runat="server"></asp:ContentPlaceHolder>
        </div>

        <div id="divMainContent">
        <asp:ContentPlaceHolder ID="cphMainContent" runat="server"></asp:ContentPlaceHolder>
        </div>
    </div>
    </form>
</body>

I then have a page that uses this master page that contains the following:

<asp:Content ContentPlaceHolderID="cphHead" runat="server">   
    <script type="text/javascript" language="javascript" >    

        function test() {
            alert("Hello World");
        }
 </script>
</asp:Content>

<asp:Content ContentPlaceHolderID="cphMainContent" runat="server">
    <fieldset>          
        <img alt="Select As Of Date" src="Images/Calendar.png" id="aAsOfDate" class="clickable" runat="server" onclick="test();" />            
        <asp:Button runat="server" CssClass="buttonStyle" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Clicked"/>            
    </fieldset> 
</asp:Content>

When I run this page and click on the image I get an "Object Expected" error.

However, if I place the test function into my Default.js external file it will function perfectly.

I can't seem to figure out why this is happening. Any ideas?

A: 

EDIT:

Try using an http analyzer like fiddler (fiddler2.com/fiddler2) to see if the script is actually loading. The issue is probably that the relative path of the aspx page is incorrect for the script you're loading. The path is always relative to the executing aspx, not the location of the Master Page. If the script isn't loaded, the function test() never exists hence the error.

Try this in your header to make sure the path is always correct

<script type="text/javascript" language="javascript" src='<%= Page.ResolveClientUrl("~/js/Default.js") %>' ></ script>

instead of

<script type="text/javascript" language="javascript" src="js/Default.js" />
Laramie
At this point I have everything else commented out for testing purposes. Originally I had four additional external js files that were being referenced for a calendar control I'm using. The other interesting thing is that this page worked perfectly until I added the master page into the mix. Once I did that this error started happening.I'm also testing this using the built in VS development server, not sure if this would make a difference.
Philter
After some further testing it seems to give me the js Object Expected error if there are any external js files referenced either in the master page or on the actual content page. If I comment all of the external includes out the embedded script works correctly.
Philter
It's tough to say without being able to look at the code, but I suspect there's something some uncompleted object in one of those files that's cascading out, causing the test() not be register as global. Have you tried just including the Default.js twice or another test script that only has some benign statement like alert('empty script'); ? If it passes add your scripts back in one at a time. If that fails, try using the Jint library to track it down. http://www.ehow.com/how_4791439_code-javascript-lint-visual-studio.html
Laramie
Found this for you as well to check the syntax of your other JavaScript files http://madskristensen.net/post/Verify-JavaScript-syntax-using-C.aspx
Laramie
Thank you for the help thus far. I still can't seem to narrow down what the issue is. I've commented out all of the external JS includes on all of my pages except for the include on the master page which contains my Default.js file. Inside of the Default.js file the only line of code I have is `alert("test")` and I still receive the same object expected error when clicking on my image that would fire the embedded JavaScript. However, when I comment the include out of the master page the embedded script works fine. I'm totally at a loss.
Philter
See edit to answer
Laramie
I actually just figured out the answer.I had to complete the `<script>` tag. So basically I changed `<script type="text/javascript" language="javascript" src="js/Default.js" />` to`<script type="text/javascript" language="javascript" src="js/Default.js" ></script>`and that solved it. I did a little looking around on it and it has to do with lazy evaluation of the `</script>` tag by IE as far as I can tell.
Philter
crazy. well I'm glad you figured it out. I didn't realize IE would be so strict. great catch.
Laramie