Javascript src属性不起作用

时间:2013-02-15 02:28:10

标签: javascript html src

这是一个非常简单的问题。网页的来源是

<script type="text/javascript" src="/jscript.js"></script>
<html><body>
<h1>It works</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>

我把js放在最开始。

在jscript.js中,它是:

<script type="text/javascript">
    document.write("test text from bill!");
</script>

但它没有显示文字。如果我将js嵌入到html中,它就可以了。

奇怪的是,当我从webbrowser直接访问jscript.js时,内容如下:

<script type="text/javascript" src="/jscript.js">
</script><script type="text/javascript">
document.write("test text from bill!"); 
</script>

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:6)

您的javascript文件中不需要<script type="text/javascript"></script>。事实上,这就是破坏一切的东西。删除它们,它应该正常工作。

答案 1 :(得分:1)

您不应在JavaScript文件中包含标记。

即使你删除它们,你的文本也会在页面中写得太早,所以我不确定它是否会正常显示。目前,您正在编写之前的文本 <html>

我不确定您的<script>位置是否有效。此外,您已经排除了文档的<head>,这是必需的。

您的网页写入body的正确结构将是:

<html>
<head>

</head>
<body>
    <script type="text/javascript" src="/jscript.js"></script>
    <!-- your script will write the content right here -->

    <h1>It works</h1>
    <p>This is the default web page for this server.</p>
    <p>The web server software is running but no content has been added, yet.</p>
</body>
</html>