dart2js compiled code running in an unsupported browser

Issue

What happens if I try to run my generated javascript code in an unsupported browser? Like IE6?

I don’t want to end up in a situation that my users will see a partly working broken app. Is there a way how to ensure that the dart/javascript will run only if the browser is supported and have my app degrade gracefully to some html banner “use a newer browser please” if it is not?

Solution

You could use the browser detector script here: http://www.quirksmode.org/js/detect.html

I wrote this code from the top of my head, I don’t have a testing machine at my disposal right now:

var isNewFirefox = BrowserDetect.browser === 'Firefox'  && BrowserDetect.version >= 7;
var isNewChrome  = BrowserDetect.browser === 'Chrome';
var isNewIE      = BrowserDetect.browser === 'Explorer' && BrowserDetect.version >= 9;
var isNewSafari  = BrowserDetect.browser === 'Safari'   && BrowserDetect.version >= 5.1;
var isNewOpera   = BrowserDetect.browser === 'Opera'    && BrowserDetect.version >= 12;

if (isNewFirefox || isNewChrome || isNewIE || isNewSafari || isNewOpera) {
    var script = document.createElement('script');

    if (navigator.webkitStartDart || navigator.startDart || navigator.mozStartDart || navigator.oStartDart || navigator.msStartDart) {
        // Load Dart code!
        script.setAttribute('type', 'application/dart');
        script.setAttribute('src', 'http://.../foo.dart');
    } else {
        // Load dart2js code!
        script.setAttribute('src', 'http://.../foo.js');
    }

    document.body.appendChild(script);
} else {
    alert('Application wont work');
}

The version information can be found on: http://www.dartlang.org/support/faq.html#what-browsers-supported

Dart VM detection: http://www.dartlang.org/dartium/#detect

Answered By – Kai Sellgren

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.