views:

33

answers:

3

For some complicated reason, I need to mark some Javascript as "special", like this:

<script type="text/javascript" someattribute="special">
  var special = "I'm special!";
</script>

<script type="text/javascript" someattribute="special" src="special.js">
</script>

Is it possible to do this in way that complies with XHTML standards? According to http://www.w3schools.com/tags/tag_script.asp, all attributes for the script tag have very specific functions. But is there a workaround?

The idea is to pick up the tags as XML elements and put them in anther page, at server level, before it gets to the browser, so I need the special mark in the actual XML of the page. Adding it once the page has loaded, at browser level, using Javascript, will not work.

Any ideas?


Edit:

For the sake of standards-compliance, I can't use HTML5. The whole system I'm trying to be compliant with is XHTML 1.0.

Now that I've had time to think about it, I think that adding a GET variable or an anchor in the src of the script might just do the trick. For example, instead of the previous example, do

<script type="text/javascript" src="special.js?special"></script>

or

<script type="text/javascript"  src="special.js#special"></script>

I'll try it now.

A: 

If you're using HTML5, you can do this:

<script data-special='true'>
</script>

and it'll validate. However, other than failing validation, you can get away with putting arbitrary attributes on tags and it'll generally work. Using the "data-" HTML5 convention is a safe way to go because you've got a 0% chance of accidentally triggering some weird behavior.

Note that with HTML5 you don't need the "type" attribute, and in fact it's deprecated in trendy Javascript circles.

Pointy
A: 

Use data-* attributes.... They are standards-compliant:

<script data-foo="bar">
Šime Vidas
A: 

With XHTML you are able to create your own DTD and with your own DTD you can easily add an extra attribute to the script tag. However... since you probably want to use this in a real life website this won't work because Internet Explorer does not support that.

So I think these are your choices:

  • wrap a tag around it to mark it as special (completely standards compliant, works in every browser, but might not work for your check)
  • adhere to the standards and create your own DTD which will somewhat break IE support
  • ignore the standards and just add some attribute (noone besides the W3 validator will really care)

If you pick the last option, I suggest using the id attribute. Which, although not supported for script tags, is supported in both html and xhtml in general so browsers won't care too much about it and most validators won't care either.

WoLpH
Is there a tag I can write around it in the head, or just in the body?
eje211
That would be something for just the body. Inside the head it's a bit more difficult.
WoLpH