html - Javascript src property doesn't work - Stack Overflow

It's a very simple one. The source of the webpage is<script type="textjavascript" sr

It's a very simple one. The source of the webpage is

<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>

I put the js at the very beginning.

in jscript.js, it's:

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

But it doesn't show the text. If I embed the js into html, it works.

And it's strange that when I directly access jscript.js from a webbrowser, the content is like:

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

Can anybody help?

It's a very simple one. The source of the webpage is

<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>

I put the js at the very beginning.

in jscript.js, it's:

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

But it doesn't show the text. If I embed the js into html, it works.

And it's strange that when I directly access jscript.js from a webbrowser, the content is like:

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

Can anybody help?

Share Improve this question asked Feb 15, 2013 at 2:28 billtianbilltian 6,8514 gold badges21 silver badges28 bronze badges 3
  • a Script tag with a src attribute is not an include. As such, there's no document for the 'document.write' to write too. Even if there was, it'd write it before the opening HTML tag, which would also be pointless. Also your .js file has HTML tags in it, also wrong. – DA. Commented Feb 15, 2013 at 2:30
  • By using John's answer, it works now. the document.write can also writ the text to the browser correctly. Thanks anyway – billtian Commented Feb 15, 2013 at 2:52
  • @billtian: Just because it works doesn't mean it's correct. It's not valid to have the script where you have it. The browser is performing corrections, but it's not guaranteed. – the system Commented Feb 15, 2013 at 3:05
Add a ment  | 

2 Answers 2

Reset to default 6

You don't need the <script type="text/javascript"> or </script> in your javascript file. In fact, that's what is breaking everything. Remove them and it should work properly.

You shouldn't include tags in the JavaScript file.

Even if you remove them, your text will be written too early in the page, so I'm not certain that it will show up properly. Currently you're writing the text before the <html>.

I'm not certain your <script> location is even valid. Also, you've excluded the <head> of the document, which would be required.

Proper structure for your page to write to the body would be:

<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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744345535a4569659.html

相关推荐

  • html - Javascript src property doesn&#39;t work - Stack Overflow

    It's a very simple one. The source of the webpage is<script type="textjavascript" sr

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信