or
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<script></script> or <script />? [duplicate]

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?

like image 248
David S. Avatar asked May 11 '11 06:05

David S.


People also ask

Can I duplicate a script in final draft?

If you're unsure about how you want to proceed, you can go to the Scripts list and duplicate the file in question. The duplicate file will not be synched, and therefore it will retain your changes.

Can I have 2 scripts in HTML?

An HTML page can contain multiple <script> tags in the <head> or <body> tag. The browser executes all the script tags, starting from the first script tag from the beginning.

What is the purpose of the script and </ script tags?

The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

What are the two types of scripts in JavaScript?

There are two main types of scripting languages: server-side and client-side.


2 Answers

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.

like image 97
Guffa Avatar answered Sep 28 '22 05:09

Guffa


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

like image 45
Headshota Avatar answered Sep 28 '22 03:09

Headshota