user agent - Newbie to Javascript: iOS, Android, or Desktop Javascript Optimization -


i have been getting javascript. not lot, i'd optimization , tips on if there'd better way handle script:

    <script type="text/javascript">     function fnisapplemobile()      {        if (navigator && navigator.useragent && navigator.useragent != null)         {         var struseragent = navigator.useragent.tolowercase();         var arrmatches = struseragent.match(/(iphone|ipod|ipad)/);         var arrmatches2 = struseragent.match(/(android)/);         if (arrmatches) {              document.write("<b>you on ios!</b>"); }         else if (arrmatches2) {              document.write("<b>you on android!</b>"); }         else {               document.write("<b>you on computer.</b>")}        }     }  var bisapplemobile = fnisapplemobile(); </script> 

the way script ends cannot optimal. advice?

thank patience.

a function use it, should return value. in case might thing you're expecting boolean more specific.

you improve code this:

<script type="text/javascript"> function fnisapplemobile()  {    var blnreturn = false;     if (navigator && navigator.useragent && navigator.useragent != null)     {     var struseragent = navigator.useragent.tolowercase();     var arrmatches = struseragent.match(/(iphone|ipod|ipad)/);     if (arrmatches) {          blnreturn = true;     }    }     // true if apple mobile; false if it's     return blnreturn; }  // store in variable var bisapplemobile = fnisapplemobile();  // or use directly need it: if (fnisapplemobile()) {     alert("cool gadget"); } </script> 

my advise; never use document.write. not when you're dealing dom @ least. if need output text, try output text inside dom node.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -