15

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

I just found a weired behavior with the script tag in HTML.

I web server is nginx, and I used FAST CGI and PHP5. I have a page.html, which looks like this:

<html>
  <body>
    <!-- <?php echo 'i am going to add php code here'; ?> -->
    <script type="text/javascript" src="./my/javascript1.js" />
    <script type="text/javascript" src="./my/javascript2.js" />
  </body>
</html>

If this page is served directly from the web server, the java script works well. But if it passed to PHP5, it seems only the first java script tag is executed. But if I change the script block into:

    <script type="text/javascript" src="./my/javascript1.js"></script>
    <script type="text/javascript" src="./my/javascript2.js"></script>

Everything works again. Noticed how the tags are closed? Yeah, that is why I am asking here. What is the difference? They are supposed to have the same function/meaning. Besides, the output HTML that my web browser (Chrome/IE9) received are the same, but why treated differently?

3
  • 2
    What do you mean by "if it passed to php5" ? Commented May 11, 2011 at 6:03
  • 3
    Further reading : stackoverflow.com/questions/69913/… Commented May 11, 2011 at 6:05
  • @Nanne set a handle in Nginx so the page is processed by PHP5. Commented May 11, 2011 at 6:28

5 Answers 5

8

The script tag needs a separate closing tag to be valid code. See http://www.w3.org/TR/html401/interact/scripts.html#h-18.2.1

Some browsers will accept the self closing tag, others wont, and the HTML version that you are using also affects the result. There are not self closing tags unless you use XHTML.

Passing the file through the PHP engine should not change the result, but it's possible that it makes an attempt to correct the incorrect script tags. You should view the source in the browser to see if the tags has been changed.

Sign up to request clarification or add additional context in comments.

1 Comment

want to take a shot at answering this part - If this page is served directly from the web server, the java script works well. But if it passed to PHP5? I'm assuming he means PHP vs HTML.
4

The script tag is required to have a closing tag, even if it uses the src attribute. Avoiding it causes undesired behaviours.

Comments

0

The way your browser interprets your HTML has nothing to do with PHP since it is NOT an HTML parser. Some browsers accept it. others don't.... Also check this link. for more Q&A about the same topic

Comments

-1

It appears that your server is configured to output XHTML rather than HTML. HTML has no such things as <tag />. But XML does have them.

Check document type declaration in both cases. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" and the like at the beginning of your document.

Comments

-3

That is basically dependant upon your DocType mentioned in the HTML.

If the DocType is mentioned transitional XHTML then it would not allow the script tag to be closed like this <script ... />. It would only allow the tag to be closed like this. <script ... ></script> and so on.

If the DocTYpe is something other than XHTML it would depend on browser compatibility in that case.

For a reference, see this...

http://www.w3schools.com/tags/tag_doctype.asp

9 Comments

That is not correct. The closing tag is always required, regardless of the HTML version used.
The thing I mentioned above is 100% right (although not bookish), but it works with most of the browsers (including IE 6)
No browser that i know of allows <script ... /> in HTML of any kind. It's invalid HTML, and every browser i've ever seen treats it as equivalent to <script ... > with no closing tag, which causes varied-but-always-wrong behavior among different browsers.
BTW, w3schools sucks as a reference for stuff like this.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.