I wrote some JavaScript code to detect Opera, because my site uses a lot of HTML5 video in h.264 format, which Opera does not support. Instead of painstakingly transcoding my 100+ videos, I want to redirect Opera to a Flash version of the website.
This is the code I wrote, and it does not seem to work. Why is that? Sorry, I'm new to JavaScript.
<script type="text/javascript">
<!--
if ((navigator.userAgent.match(/Opera/9.80/i))) {
location.replace("http://mysite.com/flash");
}
-->
</script>
P.S. PHP or other server-side language is not an option.
Your regular expression is incorrect. In JavaScript the / character is used to indicate the start and end of the regular expression, so if you want to use one as part of the pattern you need to escape it:
if ((navigator.userAgent.match(/Opera\/9.80/i))) {
Not the answer you're looking for (in terms of how to test user agent) but a better solution (IMHO).
Have a look in to Modernizr and checking against video (with specific tests against h264).
Also, for what it's worth, there is a way to support video across devices.
EDIT As @PaulD.Waite pointed out, you can test support for this solely using the following code (from DiveIntoHTML5):
function VideoAndH264IsSupported(){
var v = document.createElement('video');
return !!(v.canPlayType && v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"').replace(/no/, ''));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With