javascript - Will having the <script> tag in html effect the result of the html in a browser? - Stack Overflow

So I have this code:<!doctype html><html lang="en"><head><title>Exam

So I have this code:

    <!doctype html>

<html lang="en">
<head>
    <title>Example</title>
    <link type="text/css" rel="stylesheet" href="tjg.css"/>    
</head>
<body>
    <script type="text/javascript" src="tjg.js"/>    
   <p>This paragraph is an example.</p>
</body>
</html>

And the Javascript file (tjg.js) is empty. But when I view it in all of my browsers. It just shows nothing, not the example paragraph. This only happens when the:

<script type="text/javascript" src="tjg.js"/>

is in my code. If it isn't (head/body, doesn't matter) than it will work fine. How can I fix this?

So I have this code:

    <!doctype html>

<html lang="en">
<head>
    <title>Example</title>
    <link type="text/css" rel="stylesheet" href="tjg.css"/>    
</head>
<body>
    <script type="text/javascript" src="tjg.js"/>    
   <p>This paragraph is an example.</p>
</body>
</html>

And the Javascript file (tjg.js) is empty. But when I view it in all of my browsers. It just shows nothing, not the example paragraph. This only happens when the:

<script type="text/javascript" src="tjg.js"/>

is in my code. If it isn't (head/body, doesn't matter) than it will work fine. How can I fix this?

Share Improve this question asked Dec 28, 2013 at 20:52 user2876064user2876064 151 silver badge2 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

Use

<script type="text/javascript" src="tjg.js"></script>

The /> notation doesn't work in HTML as you expect.

The script tag cannot be self closed.

Do this instead:

   <script type="text/javascript" src="tjg.js"></script> 

Also you could set your javascript tag in the <head> section instead of the body tag

<head>
    <title>Example</title>
    <link type="text/css" rel="stylesheet" href="tjg.css"/>
    <script type="text/javascript" src="tjg.js"></script>      
</head>

Yes, it does have an effect, since browsers interpret the tag

<script type="text/javascript" src="tjg.js"/>

as if the slash before “>” was not there, i.e. as simply a start tag. Everything after it will be parsed as content of that element, up to the next </script> end tag or end of data, whichever es first. In the sample case, the rest of the document is parsed that way and ignored (browsers won’t even try to process it as a script, since the element does not end).

So you really need to use a </script> end tag, even if the script element content is meant to be empty, as suggested in other answers.

This applies to actual browser behavior when using HTML parser. Formally, by SGML rules, the situation is a bit different, but this only matters in formal SGML validation (e.g., when using http://validator.w3).

When the data is served as “genuine XHTML”, with an XML content type, things are different. Self-closing actually works. But the sample code does not work when served that way: the string doctype must be in uppercase in XHTML, and the html element must have the attribute xmlns="http://www.w3/1999/xhtml". There is normally no point in serving XHTML that way on the Web. So this point is really theoretical, but it has the point: it illustrates that “self-closing tags” actually work in a very limited environment only.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信