I came across this code
<script class="example" type="text/javascript">
and was curious if there is a benefit to writing that into your code
I came across this code
<script class="example" type="text/javascript">
and was curious if there is a benefit to writing that into your code
Visual styling's probably not a great case for this. Somewhere it could be used is dynamically selecting one from many script snippets to use elsewhere, for instance with with a javascript templating language.
Contrived Handlebars example:
<script class="greeting english" type="text/x-handlebars-template">
<p>Hello {{name}}</p>
</script>
<script class="greeting spanish" type="text/x-handlebars-template">
<p>Hola {{name}}</p>
</script>
...
var greeting_template = Handlebars.compile($('script.greeting.' + language).html());
$('#header').append(greeting_template(user));
I just ran a quick test with this markup:
<!DOCTYPE html>
<html>
<head>
<style>
.foo {
display: block;
border: 2px solid red;
width: 10px;
height: 10px;
}
</style>
</head>
<body>
<script class="foo" type="text/javascript">
alert("can you see me?");
</script>
after the script
</body>
</html>
The result was a red block on the screen and the contents of the script tag visible when ran in Chrome. IE does not render the script content visibly at all. So <script>
can be treated like any other tag, at least in Chrome. I'd argue that's an oversight on Chrome's part. This is Chrome 10.0.648.204 on 32bit Windows 7.
EDIT: Firefox 4 also renders the same thing.
EDIT2: Possible use case? Use it as a "show source" for script on your page to show people how it works, perhaps on a blog about JavaScript?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
</head>
<body>
<script class="foo" type="text/javascript">
function foobar() {
var a = 1;
}
</script>
after the script
<a href="#">show me the script</a>
<script type="text/javascript">
$('a').click(function(event) {
event.preventDefault();
$("<div>").html($(".foo").text()).appendTo($("body"));
});
</script>
</body>
</html>
According to w3c Standard, as described by w3schools.com a script tag does indeed support Global Attributes, one of which is the 'class' attribute. Thus it is perfectly "legal" to have script tags with a specific class attribute, and browsers that break this are not fully w3c compliant. The Global Attribute 'id' is also supported by the script tag.
As for the possible utility of having script tags with a 'class' or 'id' there would need to be a very specific usecase for which traversing a w3c document or a DOM abstraction, before sending it to be rendered by a client browser, is handled. In this case the means of making changes to a document could be done using css style selection and having 'id' and 'class' for grouping scripts could be useful for a number of different reasons.
It is a narrow and specific case, but one that I am actually involved in at this very moment. To say there is no usefulness to it is narrow minded and to say it is illegal by the standards is false. It may not be supported on all browsers, but it is not uncommon for browsers, even modern ones, to interpret the w3c standard differently and implement their version of standards support in their own way.
The only instance I can think of when this would be useful is when script tags are dynamically added to a page (comet functionality), as that is one of the few times when you might actually interact with a script tag.
The only benefit I can think of for having a class
property for a script is that jQuery may be able to select the element and do some manipulation, for example duplicate it.
It is useful when using the script tag itself as a reference for adjacent selectors.
i.e.
.example + iframe {height: none;}
<script class="example" type="text/javascript" src="//weird-addon.js">
</script>
In the above example, I am loading a script onto the page. The script is writing an element, in this case an iframe, directly to the DOM.
I don't want it to be shown immediately, so I am using the script class to select the adjacent iframe to hide it on page load.
Controlling the actual script tag display might not be itself useful, but used in combination with adjacent selectors, it does merit its uses.
You can probably use class or id to erase your script written inside it for security reason like.
<script id="erasable" type="text/javascript">
//your code goes here
document.getElementById('erasable').innerHTML = "";
</script>
Adding a class or an ID let you select the script tag with Javascript, and then get its content, for example a JSON object, or anything. This way you avoid creating a global variable (using jQuery here) :
<script id="datas" type="text/javascript">
{"id": 1}
</script>
<script type="text/javascript">
// This part of the code should be in a JS module
// To avoid creating a global "object" variable
var object = JSON.parse($('#datas').html());
console.log(object.id); // Log "1"
</script>
Javascript templating engines use this technique too, for example (Handlebars):
<script id="entry-template" type="text/x-handlebars-template">
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{{body}}}
</div>
</div>
</script>
More to read on this question : Is there a standard for embedding JSON in HTML?
I have found adding a class to a script tag useful in developing a code editor that allows editing the javascript in specific blocks on a page,
<div id="idForABlock">
<script class="jsCustomJavascript">
//user generated code
</script>
</div>
A immediate benefit that comes to my mind is that Javascript/Jquery/etc can now select that script element by the class name.
I don't see any benefit since:
I think it is not exactly correct to use the class attribute. According to w3schools the script-tag does not support Standard Attributes to which the class property belongs. So jQuery might select it when using filtering for the class but you have no guarantee. Also it's not sure if the behaviour is the same in all browsers.
Ancient thread, but I'd like to add that today, in 2021, no more "only jQuery", since we live in Cloudflare workers world. So naming a script element does help to target them. For example, to update cache busting (CB) query when a file is updated and you're caching full HTML with Cloudflare, you need to update the CB query with CF workers script.
<script class="xyz-cb-query xyz-cb-vendor-js" src="/assets/vendor.js?v=938750039">
<script class="xyz-cb-query xyz-cb-app-js" src="/assets/app.js?v=938750039">
One CLASS to rule them all, one CLASS to find them, One CLASS to bring them all, and in the Cloudflare bind them. (FROM 'Lord of the Rings' WHERE CLASS = RING... ;)