Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 50 Time: 05:37 - Mar 28, 2007  

jgivoni

How do I enclose inline javascript in valid XHTML?

I know it's probably best not to put javascript directly in an html file but that's a different discussion :-)

I want to add some javascript in a <script> tag in the <head> section of an <html> document.
I want it to validate as XHTML 1.0 Strict.
The script has ampersands (&) and that is a problem with XML.

How do I enclose the code to make it valid and still make it execute properly in major browsers?

Jakob

Answer Discussion
Tutorials

 

nidhi

Date:: Apr 12, 2007

Time:: 07:54

doesnt inline javascript break the rules of xhtml

have u tried this, put your javascript in its own .js file then reference it in your xhtml document

<script language="JavaScript" src="mysexyjavascriptstuff.js"></script>

jgivoni

Date:: Apr 13, 2007

Time:: 03:42

I'm pretty sure there is a way to do it so that it doesn't violate xhtml norms.
It would propably involve putting it in <![CDATA[ tags and then escaping that tag so javascript won't choke on it and throw an error.

Obviously I can put it in an external .js file, but that's not the question :-)

Jakob

nidhi

Date:: Apr 16, 2007

Time:: 07:16

Hello Jakob, okay, so workaround a no go then :)
Right, it seems your very close on the mark with using the character data tags

How have you tried doing this in code?

eg

<script type="text/javascript" language="javascript">
/*<![CDATA[*/
document.write('Hello World!');
/*]]>*/
</script

but the wiki does say not to use document.write, instead try to use node creation methods

http://en.wikibooks.org/wiki/XML_-_Manag...

jgivoni

Date:: May 19, 2007

Time:: 07:04

Hi Nidhi,

sorry for being a bit late with my respond.

I did try your suggestion amongst other things.
Here are two other options:

1: Which uses // style javascript comments instead of /* ... */
<script type="text/javascript" language="javascript">
//<![CDATA[
document.write('Hello World!');
//]]>
</script>
I suspect the result is exactly the same as nidhi's suggestion in all browsers

2: Which use of simple html style comments (and a javascript // comment):
<script type="text/javascript" language="javascript">
<!--
document.write('Hello World!');
//-->>
</script>
I've seen this method used and suggested somewhere - and sometimes in combination with CDATA tags, but I don't really understand why it works, and it's probably also a kind of legacy approach to hide javascript from incompatible browsers.

3: The 'catch-all' bulletproof approach
<script type="text/javascript">
 <!--//--><![CDATA[//><!--
  document.title = "Foo & Bar";
 //--><!]]>
</script>
I suppose it'll work. But as the author states:
"Remember that the very concept of sending XHTML to be handled by an HTML engine is essentially a hack and cannot be done flawlessly."
Source: http://www.webdevout.net/articles/escapi...

For me the first approach did the trick in the major browsers.

Thanks, nidhi

Question Answered

This question has been closed, and points have been rewarded to the following experts:


nidhi: 50

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

jgivoni

Date:: Jan 14, 2009

Time:: 08:16

This is the simple and robust approach that has served the purpose for me until now:

<script type="text/javascript" language="javascript">
//<![CDATA[
document.write('Hello World!');
//]]>
</script>

Explanation:
the non-xhtml part of the script has to be wrapped in <![CDATA[...]]> tags in order to validate.
These tags, which are invalid javascript, are then commented out with // (which is still valid xhtml).


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

kangang

Date:: Dec 25, 2010

Time:: 22:15

Hey, guys, I want to recommend you a PC cleaner, the software "tuneup360". It's very easy to handle and of course very powerful, you can have try, I'm sure it can improve your computer performance.


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

webmaster

Date:: Mar 10, 2011

Time:: 04:53

AS mentioned above use the opening tag of <script type="text/javascript" language="javascript">

is you are using & then do the following &amp; this tells html that its valid. YOu can see examples of this in source code of my own work in progrss site http://www.nfpc.org.uk


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

wy4163523

Date:: Apr 11, 2011

Time:: 06:11

http://www.bridalweddingdresses.us/
http://www.bridalweddingdresses.us/38-we...


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10279

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es