-2

Possible Duplicate:
Why don’t self-closing script tags work?

I have a LAMP server recently installed on Ubuntu. What I am curious about is the following.

This code in file test.php works:

<html>
<head>
<script type='text/javascript'>
</script>
</head>
...

This file however does not (only shows data after the next </script> in the code)

<html>
<head>
<script type='text/javascript'/>
</head>
...

Why does PHP (or HTML) not acknowledge the end tag in the second case?

Community
  • 1
  • 1
KrNeki
  • 135
  • 8

4 Answers4

4

It is not acknowledged because:

<script> 

requires a closing

</script> 

and not just a / within the tag. Some HTML tags are like that.

RonaldBarzell
  • 3,822
  • 1
  • 16
  • 23
1

You have not properly closed you script tag. Different browsers may interpret this differently. This is especially true since you are using HTML5 document type.

You would have better luck with the self-closing script tag across browsers if you were using XHTML document type.

The best approach though for cross-browser compatibility is not to use self-closing script tags at all in any document types.

This has nothing to do at all with PHP. So not sure why that was in your title. PHP will go ahead and render your source code as is.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
0

In the second case, you have closed the script tag whilst opening it:

<script type='text/javascript'/>

I highlighted it above. If you look at the first bit of code you give, you have a script tag opening, and a closing one which is correct

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
gray
  • 798
  • 18
  • 31
0

As explained in more detail in this answer: https://stackoverflow.com/a/206409/476786 all html tags are valid self-closing tags in xhtml, i.e., application/xhtml+xml. In plain old html, text/html, it is up to the server and browser to interpret the tags, thus leading to inconsistent implementations.

Community
  • 1
  • 1
bPratik
  • 6,894
  • 4
  • 36
  • 67
  • 1
    Your answer I believe is the most correct. Some versions of Chrome supported it, some dint. Sorry to have failed to mark your answer as correct, I was new at StackOverflow. – KrNeki Jul 29 '14 at 14:39