/    Sign up×
Community /Pin to ProfileBookmark

Using <![CDATA[ ]]> instead of <!– –>, now my code isn’t working..

I put a block of code in script tags in the header of my webpage with <![CDATA[ ]]> instead of <!– –> and not it no longer works. However, I thought CDATA was the more correct way to include the code.

What’s going on? Any ideas?

Thank you all in advance!

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@scragarJan 22.2008 — [code=html]<script type='text/javascript'>
/* <![CDATA[ */
// code
/* ]]> */
</script>[/code]
only a few browsers automaticly escape the cdata tags, for the rest you want to use javascript comments on/around the tags opening and closing.
Copy linkTweet thisAlerts:
@cgm225authorJan 22.2008 — At this point in time then, doesn't that defeat the purpose? What do you use? Do you recommend CDATA or <!-- -->?
Copy linkTweet thisAlerts:
@scragarJan 23.2008 — inline javascript needs cdata tags around it, this prevents browsers attempting to interprit tags between the points:

alert box: "</script>"
[code=html]<script type='text/javascript'>
/* <![CDATA[ */
alert("</script>");
/* ]]> */
</script>[/code]

causes an error, and bizare content:
[code=html]<script type='text/javascript'>
alert("</script>");
</script>[/code]
Copy linkTweet thisAlerts:
@felgallJan 23.2008 — Inline JavaScript in HTML is CDATA by default and hence doesn't need the CDATA tag.

Inline JavaScript in XHTML is PCDATA by default and so needs the tag which will then be correctly interpreted without the surrounding comments.

If you are using XHTML 1.0 and are serving it as HTML for Internet explorer then you need both the CDATA tag (for validation and browesers that understand XHTML) and need the comments around it so that IE can handle it as HTML.

The <!-- --> comments are only needed for visitors using IE2 or Netscape 1 and are not needed if all your visitors are using more recent browsers than that.
Copy linkTweet thisAlerts:
@mrhooJan 23.2008 — The best way to include scripts is with the script element's src attribute.
Copy linkTweet thisAlerts:
@cgm225authorJan 24.2008 — Follow-up then..

I would like to have everything in an external js file. However, I need to pass a php variable to the javascript I am using (I am working on implementing someone else's slideshow here; I am passing the "count" variable), so I have resorted to putting a small block of script in the actual webpage header, and then calling the object functions later in the body of the page.

So my follow-up question is, after looking at my implementation of the javascript of that example I gave above, is there anyway to get it all in an external js file? Or do you have any other feedback on how my javascript code looks on that page?

Thank you all for your help!
Copy linkTweet thisAlerts:
@felgallJan 24.2008 — <script type="text/javascript" src="myscript.php"></script>

and put the JavaScript in myscript.php where it will first be parsed to have the PHP code substituted to generate the javaScript.
Copy linkTweet thisAlerts:
@scragarJan 24.2008 — include a .php file as a peice of javascript and serve is as text/javascript?

your external js file:[code=php]<?php
header("Content-type: text/javascript");
?>
REST of JS code as you already have it[/code]
×

Success!

Help @cgm225 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...