Authors should not specify a language attribute on a script element. If the attribute is present, its value must be an ASCII case-insensitive match for the string “JavaScript” and either the type attribute must be omitted or its value must be an ASCII case-insensitive match for the string “text\/javascript”. The attribute should be entirely omitted instead (with the value “JavaScript”, it has no effect), or replaced with use of the type attribute.<\/p><\/blockquote>\n
More recently, you’ve probably seen a lot of this:<\/p>\n
<script>\r\n \/\/some javascript here\r\n<\/script><\/code><\/pre>\nNo attributes at all. This is the HTML5 way of handling script tags that contain JavaScript. It’s just assumed that the type is text\/javascript<\/code>. If it’s not (I’ve never even seen a different type of script) you’ll need to change it with the type<\/code> attribute. I recommend this if you are using HTML5.<\/p>\nOne more weird one:<\/p>\n
<script type=\"text\/javascript\">\r\n \/\/<![CDATA[\r\n $(\"<div \/>\").appendTo(\"body\"); \/\/ Some JS with HTML in it.\r\n \/\/]]>\r\n<\/script><\/code><\/pre>\nIF<\/strong> you are using XHTML still and IF<\/strong> your JavaScript has HTML in it (or even just the < character, as might be needed in greater-than logic), you’ll need these CDATA comments around the script IF<\/strong> you care about the script passing validation (you’ll get an error like: document type does not allow element “div” here<\/strong>). AND IF<\/strong>, you are putting script literally between the opening and closing script tags, not linking to a script src<\/code>.<\/p>\nThat’s a lot of IF’s.<\/p>\n
Takeaways<\/h3>\n\n- If you are using HTML5, just use
<script><\/code>.<\/li>\n- If you are using anything older, use
<script type=\"text\/javascript\"><\/code>.<\/li>\n- If you are writing scripts for people to use on their own sites (e.g. copy-pasteable code, WordPress plugins, etc, use
<script type=\"text\/javascript\"><\/code> and CDATA.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"I got a great question from reader Josh Kreis: I’ve noticed that on a <script><\/script> tag, there are all kinds of variations that all seem to work cross-browser. What is necessary and what isn’t?<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[4],"tags":[],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"featured_media_src_url":null,"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/15276"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=15276"}],"version-history":[{"count":11,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/15276\/revisions"}],"predecessor-version":[{"id":15284,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/15276\/revisions\/15284"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=15276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=15276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=15276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}