Invalid XHTML when adding JavaScript code to HTML pages
It’s a common problem to have a hard time validating Strict (or even Transitional) XHTML when using JavaScript on pages and usually the problem is because of using in our script of identical characters as those used by HTML, such as “< ", which means "less" in JavaScript.
The solution is adding of CDATA section, in each section we'll include our JavaScript code.
E.g.:
<![CDATA[
var Ann = 12;
var John = 25;
if (Ann < John) {
window.alert("Ann is smaller than John!");
}
]]></script>
Now that we solved the page validation issue we got into another problem: the JavaScript we did write doesn’t execute! This is happened because the JavaScript code is altered by using our CDATA section tags.
The solution for this new problem is to wrap CDATA section tags into the comment tags:
/*<![CDATA[*/
var Ann = 12;
var John = 25;
if (Ann < John) {
window.alert("Ann is smaller than John!");
}
/*]]>*/</script>
Still there is another, elegant, method to avoid all these problems, by using an external script file, which in this case will not interfere with our (X)HTML tags and of course will help us later on, saving our time of coding or copying the script on all pages we want that JavaScript to apply. Using an external file for our JavaScript will save our time in code debugging as well.
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.


My coder is trying to convince me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on numerous websites for about a year and am anxious about switching to another platform. I have heard good things about blogengine.net. Is there a way I can transfer all my wordpress posts into it? Any kind of help would be greatly appreciated!
I found your blog web site on google and test a few of your early posts. Proceed to keep up the very good operate. I simply further up your RSS feed to my MSN Information Reader. Seeking ahead to reading more from you in a while!
I see no good reason from migrating to .net. Why not just install wordpress on a separate domain and then link it to the old wp db. In this way you’ll have all your articles up?
After examining a number of of the weblog posts on your website now, and I really like your manner of blogging. I bookmarked it to my bookmark web site list and shall be checking again soon. Pls try my web site as nicely and let me know what you think.
There’s nothing greater than a great brain-stimulating post. That is just exactly what you might have here. I can hear my gears creaking. I concur with this material. This is really a quite well-written quality post. Thank you.
Hi, Neat post. There is a problem with your site in internet explorer, would check this… IE still is the market leader and a big portion of people will miss your fantastic writing because of this problem.
I want to express my thanks to you just for rescuing me from this issue. Because of exploring throughout the search engines and meeting basics which are not pleasant, I believed my life was over. Being alive without the strategies to the problems you have resolved by way of your good short article is a critical case, and the ones which could have adversely damaged my career if I hadn’t discovered the blog. Your good mastery and kindness in handling all the details was tremendous. I don’t know what I would have done if I had not discovered such a point like this. I can also at this moment look forward to my future. Thanks for your time so much for this reliable and sensible help. I will not be reluctant to refer your blog to any individual who would need support on this area.
Hi, i believe that i noticed you visited my website so i got here to “return the desireâ€.I’m trying to find issues to improve my web site!I suppose its ok to use a few of your ideas!!
I consider something really special in this site.
I found the content you posted to be really helpful. I would like you to update us with more information.
My prolonged internet investigation has now been rewarded with good quality suggestions to share with my visitors.
Hey there! This is kind of off topic but I need some help from an established blog. Is it very difficult to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about creating my own but I’m not sure where to begin. Do you have any points or suggestions? Many thanks.
Hey would you mind letting me know which web host you’re working with? I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot faster then most. Can you recommend a good web hosting provider at a honest price? Kudos, I appreciate it!
Hello there! This is kind of off topic but I need some guidance from an established blog. Is it tough to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about creating my own but I’m not sure where to begin. Do you have any ideas or suggestions? With thanks.
Hey There,I like the helpful info you provide in your article. Ill bookmark your blog and check back again here regularly. I am quite certain I will learn a lot of great things, here! Good luck! Thanks.
Hi there, I just became aware of your site via MSN, and found that it’s really informative. I am going to watch out for updates. I will appreciate if you continue the good work in the future. A lot of people will benefit from your writing. Thank You!
Hello there! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My weblog looks weird when browsing from my iphone. I’m trying to find a template or plugin that might be able to resolve this problem. If you have any suggestions, please share. Cheers!
Hey! Do you use Twitter? I’d like to follow you if that would be okay. I’m definitely enjoying your blog and look forward to new posts. Regards!
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something informative to read? Many thanks!
Some really fantastic content on this web site, thanks for contribution. “A religious awakening which does not awaken the sleeper to love has roused him in vain.” by Jessamyn West. Thanks.
Hey there would you mind stating which blog platform you’re using? I’m going to start my own blog soon but I’m having a difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique. P.S My apologies for being off-topic but I had to ask!
hey there and thank you for your information – I’ve certainly picked up anything new from right here. I did however expertise several technical points using this site, as I experienced to reload the website a lot of times previous to I could get it to load correctly. I had been wondering if your web host is OK? Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and could damage your high quality score if advertising and marketing with Adwords. Well I’m adding this RSS to my e-mail and could look out for a lot more of your respective intriguing content. Make sure you update this again very soon..
Wow! This blog looks just like my old one! It’s on a entirely different subject but it has pretty much the same layout and design. Superb choice of colors!
An exceptionally nice blog you have got here. You really love your typing style and it’s very informative posts, I shall get back here again!