最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论