在外部".JS"中定义时未调用JavaScript函数文件 [英] JavaScript Function Not Called When Defined in External ".JS" File

查看:84
本文介绍了在外部".JS"中定义时未调用JavaScript函数文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML正文如下:

The body of my HTML is as follows:

<body onload="DoLoad();">
</body>


当我在HTML文件顶部的SCRIPT标记中定义DoLoad函数时,它可以正常工作.例如:


When I define the DoLoad function in the SCRIPT tag at the top of the HTML file, it works fine. For example:

<head>
    <title></title>
    <script type="text/javascript">
        function DoLoad() {
            alert("Hello");
        }
    </script>
</head>


但是,当我将函数放在名为"TestJS.js"的文件中,并使用SCRIPT标记的SRC属性对其进行引用时,它将无法运行.在这种情况下,HTML头将如下所示:


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:

<head>
    <title></title>
    <script type="text/javascript" src="TestJS.js" />
</head>


而"TestJS.js"文件将如下所示:


And the "TestJS.js" file would look like this:

function DoLoad() {
    alert("Hello");
}


关于该函数为何可以与内联JavaScript一起使用但不能与导入的JavaScript一起使用的任何想法?我宁愿在外部文件中定义我的代码,所以以这种方式工作将是理想的.

仅供参考,我正在使用Visual Studio 2008 Professional,并且正在使用ASP.NET Web应用程序.我的操作系统是Windows Vista Home Premium.我的浏览器是Internet Explorer8.


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.

推荐答案

我相信问题出在关闭脚本标记的方式上.
IE似乎需要格式正确的XML语法才能正常工作.

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

不是
<script ....... />


test.js
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



test.html

<html>
<head>
    <title></title>
    <script type="text/javascript" src="test.js"></script>
</head>
<body onload="DoLoad();">
</body>
</html>


这篇关于在外部".JS"中定义时未调用JavaScript函数文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆