Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding script JS in HTML problem

somebody can answer me why occur this problem?

With the following code my site only works in Chrome, on IE7/8 and Firefox show me only the background:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Site Title</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js" />
<script type="text/javascript" src="js/functions.js" />
</head>
...

And with this code works in Chrome, IE7/8 and Firefox:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CSite Title</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/functions.js"></script>
</head>
...

Viewing the code that don't works in Firebug I see that the file 'functions.js' is not called, I don't understand, why don't works with "<script />" and with "<script></script>" works?

like image 518
Cesar Avatar asked Dec 02 '22 06:12

Cesar


2 Answers

from this ticket (Why don't self-closing script tags work?)

(please note that in the referenced ticket the accepted answer is not actually correct)

Note that IE does not support XHTML parsing. Even if you use an XML declaration and/or an XHTML doctype, IE still parses the document as plain HTML. And in plain HTML, the self-closing syntax is not supported. The trailing slash is just ignored, you have to use an explicit closing tag.

Even browsers with support for XHTML parsing will still parse the document as HTML unless you serve the document with a xml mime type. But in that case IE will not display the document at all!

like image 70
robjmills Avatar answered Dec 04 '22 06:12

robjmills


Because <script> tag MUST have a closing tag in HTML4

Another source for information: XHTML - Is writing self closing tags for elements not traditionally empty bad practise?

like image 42
Rubens Farias Avatar answered Dec 04 '22 06:12

Rubens Farias