/    Sign up×
Community /Pin to ProfileBookmark

XHTML 1.0 and inline scripts

Hi,

I have an XHTML 1.0 Strict document below. In the bost, I have a javascript function call. Something strange happens… when the page is rendered, the browser inserts a </head><body> immediately after the script. This means that the <body> then essentially starts just before <p>Hello World 2</p>, so that’s all I see. Does anyone have any ideas?

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>

<head>
<title>Hello World</title>
<meta http-equiv=”content-type” content=”application/xhtml+xml; charset=iso-8859-1″ />
<link rel=”StyleSheet” href=”helloWorld.css” type=”text/css” media=”all” />
<script src=”demoExtra.js” type=”text/javascript” />
<script src=”browserDetect.js” type=”text/javascript” />
<script src=”demoEngine.js” type=”text/javascript” />
<script src=”demoNodes.js” type=”text/javascript” />
</head>

<body>
<p>Hello World 1</p>
<script type=”text/javascript”>
initializeDocument();
</script>
<p>Hello World 2</p>
</body>
</html>[/CODE]

Thanks,
FF.

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisDec 01.2006 — Is it a restriction with XHTML Strict that all SCRIPT tags must be in the HEAD section?

...or, is it that the initializeDocument() function is sticking those extra tags in there?
Copy linkTweet thisAlerts:
@FreedomFriesauthorDec 01.2006 — If I remove the call to this function, I still get these pesky tags being inserted :-)

BTW, I'm using the Web Developer Tools extension to Firefox to look at the "View Generated Source" to see this behavior.

I've been trawling the internet for hours now and see plenty of references to <script> tags in the body of XHTML Strict documents. I've played around with all of the recommended escaping methods, but they don't appear to make a difference.
Copy linkTweet thisAlerts:
@FangDec 01.2006 — By ecsaping I assume you mean [URL=http://www.w3.org/TR/xhtml1/#h-4.8]CDATA[/URL], although I would recommend using an external script.
Copy linkTweet thisAlerts:
@KorDec 01.2006 — By ecsaping I assume you mean [URL=http://www.w3.org/TR/xhtml1/#h-4.8]CDATA[/URL], although I would recommend using an external script.[/QUOTE]

Yes, isolate the code in CDATA section, like that:
<i>
</i>&lt;script type="text/javascript"&gt;
/*&lt;![CDATA[*/
... your code here ...
/*]]&gt;*/
&lt;/script&gt;


The reason is that in XML (and XHTML, of course) some characters ([COLOR=Red]< > & ' "[/COLOR]) are predefined entities. Among these, 2 of them are "illegal", if used in other aims by other languages: [COLOR=Blue]<[/COLOR] and [COLOR=Blue]&[/COLOR]. For instance XML interprets [COLOR=Blue]<[/COLOR] as the start of a new element.

The CDATA section will tell the parser to ignore the code lines inside it.
Copy linkTweet thisAlerts:
@CharlesDec 01.2006 — Your problem arises, I think, from the fact that XHTML and HTML are not compatable and your browser is trying to understand your mark up as HTML. Best to switch to HTML 4.01 Strict. Second best would be to switch to XHTML 1.1 and possibly changing your file name extension to ".xml". That'll force the browser to look at the mark up as XHTML but you may not like the results. Third best would be to follow the HTML campatability guidelines in the XHTML 1.0 spec. ( http://www.w3.org/TR/xhtml1/#guidelines ) In particular you need to read C.2.
Copy linkTweet thisAlerts:
@FreedomFriesauthorDec 01.2006 — Hi Everyone,

Creating an external file for the function call, and having a call to an external script worked.

So, in other words, I changed

<script type="text/javascript">

initializeDocument();

</script>

to

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

Seems strange to have an external file with just a few characters :-) But it works. Many thanks...

FreedomFries.
Copy linkTweet thisAlerts:
@KorDec 01.2006 — I guess you should write

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

The close XML end slash is to be applied only to the tags which in HTML has no end tags, but <script> has it's genuine </script> close tag...
Copy linkTweet thisAlerts:
@CharlesDec 01.2006 — That's quite odd because you're only supposed to need to do that if your script contains "<", ">" or "&" characters. And your perrfectly valid empty XHTML "script" tags should cause a problem in browsers.
Copy linkTweet thisAlerts:
@FreedomFriesauthorDec 01.2006 — Hi Kor, Thanks for catching the <script ... /> issue. It worked fine in Firefox, but then when I went to IE, I had issues. Changing to <script>...</script> fixed things in IE.

Hi Charles, I agree that it's quite odd. I was very confused. All that was within the <script></script> tags was letters, (, ), and ;. But both browsers seem to choke on it. And now both are happy when I have these function calls in external files. I really don't understand what's going on under the covers, but I guess soemthing strange is happening when the browsers try to translate the XMTHL into something they can work with?

Thanks everyone for your help. Believe me it is much appreciated.
Copy linkTweet thisAlerts:
@CharlesDec 01.2006 — I'm wondering; What exactly does "initializeDocument()" do? I'm guessing that it has some reference to some node or part of the HTML document that comes after it on the page. When the script is internal to the page those parts don't yet exist. When it is external the page finishes parsing before the external file is grabbed. Try instead and just for kicks:&lt;script type="text/javascript"&gt;
onload = initializeDocument
&lt;/script&gt;
And do read http://www.w3.org/TR/xhtml1/#guidelines !
Copy linkTweet thisAlerts:
@FreedomFriesauthorDec 01.2006 — [sorry this posting is a little rushed... as I am in between meetings]

Good point Charles.

initializeDocument() creates a table, populates that table, and renders the table. (It doesn't use document.write.)

I'll have a closer look at exactly what it does to see if there is a possibility that something in there is causing this behavior. I'll post to here when I find out...
×

Success!

Help @FreedomFries spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...