Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Line 1671 of common.inc says:
$output .= '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .'>'. $info['code'] ."</script>\n";
Should this not be changed to:
$output .= '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .'>
//<![CDATA[
'. $info['code'] ."
//]]>
</script>\n";
Comment | File | Size | Author |
---|---|---|---|
#24 | drupal6.js-inline.patch | 1.68 KB | sun |
#22 | inline-javascript.patch | 1.23 KB | Dries |
#15 | drupal.js-inline.patch | 1.46 KB | sun |
#7 | drupal.js-inline.patch | 799 bytes | sun |
#3 | js_CDATA_HEAD.patch | 823 bytes | zeta ζ |
Comments
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous commentedYup. My XHTML pages don't validate unless I can wrap my javascript in CDATA. This appears to be a problem only when my javascript also includes XHTML. For example:
Comment #2
Anonymous (not verified) CreditAttribution: Anonymous commentedI made a patch which makes valid XHTML of all javascript. It shouldn't cause problems for HTML4 themes, but I can't test every browser out there anyway.
Comment #3
zeta ζ CreditAttribution: zeta ζ commentedversion of patch for HEAD
Comment #4
Anonymous (not verified) CreditAttribution: Anonymous commentedI am not a javascript programmer, so excuse me if I don't use the right words for things.
I found a reliable work around for my purposes. The javascript needs to be wrapped in CDATA when it contains javascript strings or string literals with characters that are reserved in HTML: < > = &. Otherwise, validators want to try to validate the HTML in there.
The drupal_to_js function in common.inc contains some code for this, but that also adds the string delimiters ("). To use this exact function in my theme, I had to 'forget' to put the quotes. See:
So you don't need to add the quotes around the 'stuff' in your javascript where you'd usually do it if you were just concatenating a bunch of strings to put through drupal_add_js.
Comment #5
Steven Jones CreditAttribution: Steven Jones commentedPatch looks good, applies cleanly.
Javascript still works after applying on firefox2/win.
Comment #6
sun+1
However, based on my findings, the most backwards-compatible way is:
Comment #7
sunComment #8
quicksketchsun, the patch looks good, but could you reference the links that say this is the best (most compatible) practice? I've personally been using:
Which looks like it'd be equivalent to #3. However, browsers are finicky things.
Comment #9
quicksketchSome links I reference for the approach mentioned in #8.
http://developer.mozilla.org/en/docs/index.php?title=Properly_Using_CSS_...
http://www.alistapart.com/articles/keepelementskidsinlinewithoffspring
Comment #10
sunSorry, it seems I have recalled it wrongly. Actually, it should be:
There are many references, but the most detailed and comprehensive is http://en.wikipedia.org/wiki/XHTML (see section 3 and 4).
Comment #11
zeta ζ CreditAttribution: zeta ζ commentedI assume that the method in #10 relies on line endings to finish the comments. I would prefer a version based on that used for css (which doesn’t have // comments), if this possible, and is sufficiently compatible with browsers de antiqua:
I appreciate the method in #10 has been tested, whereas this hasn’t: Maybe any advantage is not worth it?
Comment #12
Anonymous (not verified) CreditAttribution: Anonymous commentedWhat's wrong with just encoding your javascript string literals as described in #4? If there are browsers that can't handle CDATA blocks correctly or can't handle them without excessive and unnecessary comment blocks, why not just go the way that does work reliably across browsers? (Unless it doesn't. Please inform me!)
Comment #13
Damien Tournoud CreditAttribution: Damien Tournoud commentedThe approach in #10 is well known an well tested, and works for both
text/html
andapplication/xhtml+xml
. Could someone make a patch, so that you could get this in?Comment #14
sun@bangpound: See http://en.wikipedia.org/wiki/XHTML#Common_errors
@zeta: Depends on what we want. If we do not want to support browsers prior 2000 the method in #8 would be sufficient.
Comment #15
sunComment #16
zeta ζ CreditAttribution: zeta ζ commentedI wasn’t concerned about supporting even older browsers, just the reliance on line endings to finish the comments.
Wouldnt the
/* style of comment */
be better here? The part being commented is not naturally a line by itself or a$foo = 'bar' // set value of $foo
sort of comment.
Comment #17
sunAFAIK, it is, because this prefix/suffix needs to be on a separate line and does not affect the embedded JavaScript code. Last patch in #15 already ensures this.
As far as I can understand this wrapper:
Prefix:
<!--
HTML: Hides the following CDATA construct in HTML4//
JavaScript: Comments out the rest of this line in HTML4-->
HTML: Ends previous comment in XHTML<![CDATA[
XHTML: Starting CDATA construct in XHTML//>
JavaScript: Comments out the rest of this line in XHTML<!--
Hides JavaScript code from browsers that do not support JavaScriptSuffix:
//
JavaScript: Comments out the rest of this line in HTML4-->
HTML: Ends hiding of JavaScript code from browsers that do not support JavaScript<!]]>
XHTML: Closing CDATA construct in XHTMLComment #18
Anonymous (not verified) CreditAttribution: Anonymous commented@sun i don't understand why you're posting that link. my question is not about which XHTML comment style to use. my question is why not just encode the offending characters that cause validation to fail rather than use obtuse hack-like comment syntax to make the most non-compliant browsers behave?
Comment #19
sunEscaping characters in the script would be non-trivial and may break some scripts. Also, your proposed method in #4 does not work for HTML4 and older browsers that don't support JavaScript at all, because the script won't be commented out at all.
Like Damien mentioned, the method in #10 resp. #15 (patch) is well known, well tested, and known to work.
Please test.
Comment #20
yhager CreditAttribution: yhager commentedThe patch in #10 looks subtle enough. I wish it could land for versions prior 7.x as well..
Subscribing.
Comment #21
sunI think you've meant #15. Let's get this sucker in (and backport it later on).
Comment #22
Dries CreditAttribution: Dries commentedI've committed a modified patch to CVS HEAD. I simply added some code comments. Patch attached so it can be reviewed and/or backported to Drupal 6. Lowering version to Drupal 6 ...
Comment #23
Gábor HojtsyPatch is not in unified patch form and does not apply to 6.x.
Comment #24
sunComment #25
Gábor HojtsyThanks, committed to Drupal 6!
Comment #26
Anonymous (not verified) CreditAttribution: Anonymous commentedAutomatically closed -- issue fixed for two weeks with no activity.