Connecting Tech Pros Worldwide Forums | Help | Site Map

Perferred way to embed javascript into xhtml

howa
Guest
 
Posts: n/a
#1: Apr 15 '07
1.

<script type="text/javascript">
alert("test");
</script>

2.

<script type="text/javascript">
<!--//
alert("test");
//-->
</script>

3.

<script type= "text/javascript">/*<![CDATA[*/
alert("test");
/*]]>*/</script>



which one you prefer? (assume xhtml transitional)


thanks.


Randy Webb
Guest
 
Posts: n/a
#2: Apr 15 '07

re: Perferred way to embed javascript into xhtml


howa said the following on 4/15/2007 4:07 AM:
Quote:
1.
>
<script type="text/javascript">
alert("test");
</script>
2.
<script type="text/javascript">
<!--//
alert("test");
//-->
</script>
3.
<script type= "text/javascript">/*<![CDATA[*/
alert("test");
/*]]>*/</script>
which one you prefer?
<script type="text/javascript" src="myExternalFile.js"></script>
Quote:
(assume xhtml transitional)
xHTML isn't a good choice on the web though.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
howa
Guest
 
Posts: n/a
#3: Apr 15 '07

re: Perferred way to embed javascript into xhtml


On 4月15日, 下午10時13分, Randy Webb <HikksNotAtH...@aol.comwrote:
Quote:
howa said the following on 4/15/2007 4:07 AM:
>
>
>
>
>
Quote:
1.
>
Quote:
<script type="text/javascript">
alert("test");
</script>
2.
<script type="text/javascript">
<!--//
alert("test");
//-->
</script>
3.
<script type= "text/javascript">/*<![CDATA[*/
alert("test");
/*]]>*/</script>
which one you prefer?
>
<script type="text/javascript" src="myExternalFile.js"></script>
>
assuming the scripts will only be called once for that page, external
JS seems not a good solutions.
Quote:
Quote:
(assume xhtml transitional)
>
xHTML isn't a good choice on the web though.
>
maybe until HTML5 ?


Richard Cornford
Guest
 
Posts: n/a
#4: Apr 15 '07

re: Perferred way to embed javascript into xhtml


howa wrote:
Quote:
1.
>
<script type="text/javascript">
alert("test");
</script>
Because in XHTML the content of a script element is PCDATA the above is
completely acceptable, but if the script contained characters that were
meaningful to the XML parser they would need to be transformed into
their corresponding mark-up entities. Most real scripts would contain
such characters.
Quote:
2.
>
<script type="text/javascript">
<!--//
alert("test");
//-->
</script>
In PCDATA processing instructions are interpreted as processing
instructions and so comments they contain are interpreted as comments.
Comments may be stripped out by an XML parser but even if they were not
there should be no expectation of their contents being passed on to a
javascript interpreter. The above is just wrong in an XHTML (and all
otehr XML) context(s).
Quote:
3.
>
<script type= "text/javascript">/*<![CDATA[*/
alert("test");
/*]]>*/</script>
Wrapping <![CDATA[ ... ]]around the script contents means that
characters that may be significant to the mark-up will be disregarded
(except the "]]>" sequence, which is a potentially valid construct in
javascript, such as in - if(a[b[c]]>2){ ... } -)).

It is utterly redundant to javascript style multi-line comments around
the CDATA mark-up in an XHTML document.
Quote:
which one you prefer? (assume xhtml transitional)
The preferable approach is to realise that while IE browsers do not
support XHTML that mark-up flavour has no value in a commercial context
as something to be sending to web browsersl. And so use only HTML, where
the contents of SCRIPT elements are CDATA by definition.

If XHTML is to be used the only sensible option is to import the scripts
into the document, and so avoid all questions of what the XML parser may
make of the characters used.

Richard.

Randy Webb
Guest
 
Posts: n/a
#5: Apr 15 '07

re: Perferred way to embed javascript into xhtml


howa said the following on 4/15/2007 2:01 PM:
Quote:
On 415, 銝1013, Randy Webb <HikksNotAtH...@aol.comwrote:
Quote:
>howa said the following on 4/15/2007 4:07 AM:
>>
>>
>>
>>
>>
Quote:
>>1.
>><script type="text/javascript">
>> alert("test");
>></script>
>>2.
>><script type="text/javascript">
>><!--//
>> alert("test");
>>//-->
>></script>
>>3.
>><script type= "text/javascript">/*<![CDATA[*/
>> alert("test");
>>/*]]>*/</script>
>>which one you prefer?
><script type="text/javascript" src="myExternalFile.js"></script>
>>
>
assuming the scripts will only be called once for that page, external
JS seems not a good solutions.
Say again? External scripts with xHTML are *always* a good solution.
Quote:
Quote:
Quote:
>>(assume xhtml transitional)
>xHTML isn't a good choice on the web though.
>>
maybe until HTML5 ?
That isn't up to me, it isn't even up to the W3C, it is up to the people
in Redmond Washington, USA.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Closed Thread


Similar JavaScript / Ajax / DHTML bytes