0

I have a Site.Master in my ASP.NET project which defines a HEAD section as follows

<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title id="MasterTitle">MyApp</title>
    <link rel="icon" href="Content/icon.ico" type="image/x-icon" />
    <link href="Content/mycss.css" rel="stylesheet" type="text/css" />
    <script src="Content/mycode.js" type="text/javascript"></script>
</head>

In the mycode.js file, I have a function called GetSels();

function GetSels()
{
//do stuff
}

If the GetSels function is defined in Site.Master, GetSels is callable. If it's in mycode.js, it's not.

Every code example I've seen seems to say this should work.

What am I doing wrong?

3
  • Set an alert("It's loaded"); in your external .js file, just to make sure it is loading. Commented Mar 31, 2010 at 5:19
  • Yep. the alert fired... the actual call to GetSels is being made in a another page that has uses site.master. Is this a problem? Commented Mar 31, 2010 at 5:27
  • OK. That's freakin' bizarre. If I have the alert in the .js file, it works, but if I don't it doesn't! Commented Mar 31, 2010 at 5:31

2 Answers 2

2

This should really work perfectly as I have done this multiple times myself.

Check that the code in your external javascript file runs correctly on page load, this is just to make sure that it is indeed being loaded correctly into your document. For example set an alert("It's loaded"); in your external .js file.

Sign up to request clarification or add additional context in comments.

5 Comments

This seems to have fixed it Jay. I don't know why though... maybe it's a visual studio debugger thing?
Hey WOPR, it sounds like there was something else at play here, as setting an alert would not have fixed the problem, rather just let you know that the file itself was being loaded. I'd say some strange caching was the cause of this (especially if you were using IE, which has horrific javascript caching which you have to clear manually)
yup. I can remove the alert and it still works now. IE>>Trash. Install Chrome.
Yep, that's fixed. Thanks again for your help.
Good to hear, if you're looking at changing browsers and you're doing a lot of web development, take a look at Firefox with FireBug installed. It will help you clear up most problems like this in a matter of seconds. Though outside of web development I'd choose Chrome over Firefox personally.
0

have you checked that your reference to mycode.js is correct? if your using a relative path try "~/Content/mycode.js" in your reference.

1 Comment

The file is in the same folder as the css file. The css file gets processed properly so I presume it's correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.