views:

502

answers:

2

Hi, i read about the yield keyword in javascript and i need to use it in my project. I read that this keyword has been implemented starting from a certain version of JS so i think that old browsers don't support it (right?).

Is there a way to check if the yield keyword is supported? or at least is there a way to check if the version of JS is greater or equal than the one that implements that keyword (1.7)?

+2  A: 

Strictly speaking, only Mozilla Browsers support JavaScript. All browsers should support ECMAScript and older versions of JavaScript are implementations of ECMAScript.

This site lists what versions of Javascript are supported on what versions of browser.

MSIE uses JScript. JScript does not have yield in it. Therefore, using yield will limit the browser support for your page.

Try https://developer.mozilla.org/en/New_in_JavaScript_1.7 for info on using JavaScript 1.7

Dancrumb
So there's no way to check it without browser sniffing?
mck89
+3  A: 

yield introduces new syntax to JavaScript. You won't be able to use yield unless you have specified that you want the new JavaScript syntax, by including a version number in the script type attribute(*).

When you specify a script version, only browsers that support the given version will execute the block at all. So only Firefox, and not IE, Opera or WebKit, will execute the top block in:

<script type="text/javascript;version=1.7">
    function x() {
        yield 0;
    }
    var canyield= true;
</script>
<script type="text/javascript">
    if (!window.canyield) {
        // do some fallback for other browsers
    }
</script>

(*: note that the type and version specified in the type attribute exclusively determines whether external scripts are fetched, executed, and the mode for the execution. The Content-Type of a script is, unfortunately, completely ignored.)

bobince
Thanks this is very useful
mck89