Register  |  Login

Question Details    

   Question

Time: 05:37 - Mar 28, 2007     Asked by: jgivoni      Status: Answered      Points: 50   

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

Ask a New Question

Become a Quomon Expert

Current Categories

 

Other Questions Needing Answers


   

Answer Discussion
Answer Discussion
Answer Summaries
Answer Summary
 
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>

Expert:

nidhi

Date:

Apr 12, 2007

Time:

07:54

 

Votes: Good (0) | Bad (0)
Login to rate this answer

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

Expert:

jgivoni

Date:

Apr 13, 2007

Time:

03:42

 

Votes: Good (0) | Bad (0)
Login to rate this answer

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...

Expert:

nidhi

Date:

Apr 16, 2007

Time:

07:16

 

Votes: Good (0) | Bad (0)
Login to rate this answer

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

Expert:

jgivoni

Date:

May 19, 2007

Time:

07:04

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Question Answered

This question has been answered, 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 an Answer Summary for this question by clicking on the "Answer Summaries" Tab.

 
No summaries have been submitted yet. Want to be the first?



Respond to 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:


Forgotten Password

 

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:


Forgotten Password

   

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


All Questions


Language Options

English:

www.quomon.com

Español:

www.quomon.es

Quomon Blog

blog.quomon.com

Sponsors

Questions and Answers Software
Real Estate Postcards
Marketing Fulfillment