language versioning

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

language versioning

Neil Mix
Is there any runtime indicator of the language version for backward-
compatibility checks?  I'm considering this in the context of web  
pages, where you may want to include different scripts based on what  
the browser supports.  For example:

   if (__JAVASCRIPT_VERSION__ >= 2) {
     document.write('<script src="js2-code.js"><\/script>');
   } else {
     document.write('<script src="js2-compiled-as-js1-code.js"><\/
script>');
   }

If no such version indicator exists, the alternative mechanisms to  
determine capabilities/version would be:
- using a try/catch block to call eval() with version 2 code
- user-agent sniffing
- the (deprecated) "language" attribute of the script tag

Any other options I'm missing?  Of the given options, I think I  
prefer the idea of a runtime indicator.

Reply | Threaded
Open this post in threaded view
|

Re: language versioning

Brendan Eich-2
On Jan 14, 2007, at 12:43 PM, Neil Mix wrote:

> Is there any runtime indicator of the language version for backward-
> compatibility checks?  I'm considering this in the context of web  
> pages, where you may want to include different scripts based on  
> what the browser supports.  For example:
>
>   if (__JAVASCRIPT_VERSION__ >= 2) {
>     document.write('<script src="js2-code.js"><\/script>');
>   } else {
>     document.write('<script src="js2-compiled-as-js1-code.js"><\/
> script>');
>   }

You might test for a new ES4 method or property, e.g. Date.nanoAge.  
Better if we could define something like __ES4__ -- I will put it on  
the agenda for the next TG1 phone conference.  Thanks.

> If no such version indicator exists, the alternative mechanisms to  
> determine capabilities/version would be:
> - using a try/catch block to call eval() with version 2 code
> - user-agent sniffing

In my view, user-agent sniffing is likely to be required for a good  
while, as it was if you wanted to serve try/catch/finally statements  
to browsers after ES3 was adopted but before it became well-supported  
by almost all browsers likely to hit a server.

> - the (deprecated) "language" attribute of the script tag

The "type" attribute that replaces "language" may have a "version"  
MIME type parameter -- see http://www.ietf.org/rfc/rfc4329.txt. This  
RFC requires user agents not implementing a version to treat the  
script tag's content as unhandled, but right now too many browsers  
fail to respect "version".  As part of the ES4 spec, we have talked  
about buttressing the normative specification in RFC 4329 with a  
reference and some strong language.

> Any other options I'm missing?  Of the given options, I think I  
> prefer the idea of a runtime indicator.

In light of the alternative,

<script src="js2-code.js" type="application/javascript;version=2"></
script>
<script>
   if (!js2_code_flag)
     document.write('<script src="js2-compiled-as-js1-code.js"><\/
script>');
</script>

could be done with a js2_code_flag set to true by js2-code.js. It's  
still ugly (uglier than your double-write original, arguably), but it  
avoids one write of a script tag, and does so for the forward version.

/be