Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
The body of my HTML is as follows:
HTML
<body onload="DoLoad();">
</body>

When I define the DoLoad function in the SCRIPT tag at the top of the HTML file, it works fine. For example:
HTML
<head>
    <title></title>
    <script type="text/javascript">
        function DoLoad() {
            alert("Hello");
        }
    </script>
</head>

However, when I put the function in a file called "TestJS.js" and reference it using the SRC attribute of the SCRIPT tag, then it doesn't run. In that scenario, the HTML head would look like this:
HTML
<head>
    <title></title>
    <script type="text/javascript" src="TestJS.js" />
</head>

And the "TestJS.js" file would look like this:
JavaScript
function DoLoad() {
    alert("Hello");
}

Any idea of why the function would work with the inline JavaScript but not with the imported JavaScript? I would rather have my code defined in an external file, so getting it to work that way would be ideal.

FYI, I am using Visual Studio 2008 Professional and I am working with an ASP.NET Web Application. My operating system is Windows Vista Home Premium. My browser is Internet Explorer 8.
Posted
Updated 22-Jun-11 10:20am
v3

1 solution

I believe the problem comes from the way that you close the script tag.

IE seems to need well formed XML syntax to work properly.

I.e <script ......></script>

NOT
<script ....... />


test.js
function DoLoad()
{
	alert("Hello");
}


test.html
<html>
<head>
    <title></title>
    <script type="text/javascript" src="test.js"></script>
</head>
<body onload="DoLoad();">
</body>
</html>
 
Share this answer
 
Comments
AspDotNetDev 22-Jun-11 16:21pm    
Seems my comment that I posted (rightly) got deleted. Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900