Select Categories Below
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.
Status: Closed Points: 50 Time: 05:37 - Mar 28, 2007
jgivoni
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
Categories:
Add Categories
When adding more than one category, separate them with commas.
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>
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
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...
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:
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?
Enter your email address below and we will resend your login information to you.
Login Information Sent
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.
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 & 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
wy4163523
Date:: Apr 11, 2011
Time:: 06:11
http://www.bridalweddingdresses.us/ http://www.bridalweddingdresses.us/38-we...
Questions
Hi is there a way to retrieve the loop count in XSLT
how do repair this problem: every times i opened my laptop it will suddenly n...
How i get the hp pavilion dv6-3054tx laptop bottom base assembly as a sparepa...
I don't have AOL browser but privacy scan shows hundreds of files/cookies fro...
wireless keyboard has stopped working
need validation controller for textbox in java
You have 100 characters to use
Rank
Expert
Points
1.
10279
2.
6493
3.
5596
4.
4848
5.
3487
6.
2840
7.
2770
8.
2303
9.
1820
10.
917
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 it
Language Options
English:
Español:
Sponsors
Questions and Answers Software