Detect IE10 using JavaScript

Here are possible ways to detect IE10 browser using Javascript:

Though the following aren’t perfect solution it could be a better start.

Method 1:


var ie10 = (navigator.userAgent.match(/Trident\/[6]/i));

Method 2:


var ie10 = (navigator.userAgent.match(/MSIE 10/i));

Method 3:



<!doctype>
<html>
<head>
<!--[if (gt IE 9)]><script type="text/javascript">window.ie10 = true;</script> <![endif]-->
</head>
<body>

<script type=”text/javascript”>
if(window.ie10)
document.write(‘IE 10 detected.’);
else
document.write(‘Browser is NOT IE10.’);
</script>
</body>
</html>

Advertisements

Generating Unique Strings in Javascript

Here is a simple way to generate a unique string in javascript.

    var ts = (new Date()).getTime().toString();
    var url = &amp;quot;ajaxHandler.php?param1=value1&amp;amp;amp;paramn=valuen&amp;amp;amp;t=&amp;quot; + ts;
    //do ajax call...

This method is very helpful for ajax developers, to overcome the problem of cached ajax requests in IE.

Update:
Here’s another way of creating unique strings

String.unique = String.guid = String.uid = String.uuid = function(){
    var idx = [], itoh = '0123456789ABCDEF'.split('');

    // Array of digits in UUID (32 digits + 4 dashes)
    for (var i = 0; i &lt; 36; i++) { idx[i] = 0xf &amp; Math.random() * 0x10; }
    // Conform to RFC 4122, section 4.4
    idx[14] = 4; // version
    idx[19] = (idx[19] &amp; 0x3) | 0x8; // high bits of clock sequence

    // Convert to hex chars
    for (var i = 0; i &lt; 36; i++) { idx[i] = itoh[idx[i]]; }

    // Insert dashes
    idx[8] = idx[13] = idx[18] = idx[23] = '-';

    return idx.join('');
}

Sample Use:

   var id=String.guid();
   va u = String.uuid();

TextArea SelectedText, SelectionStart, SelectionEnd, SelStart, SelEnd (IE and FireFox)

Simple way of getting the html textarea’s selection properties selectedText, selectionStart, selectionEnd:

Javascript:

 function getTextAreaSelection() {
   var textArea = document.getElementById('textarea1');
   if (document.selection) { //IE
       var bm = document.selection.createRange().getBookmark();
       var sel = textArea.createTextRange();
       sel.moveToBookmark(bm);
     
       var sleft = textArea.createTextRange();
       sleft.collapse(true);
       sleft.setEndPoint("EndToStart", sel);
       textArea.selectionStart = sleft.text.length
       textArea.selectionEnd = sleft.text.length + sel.text.length;
       textArea.selectedText = sel.text;  
   }
   else if (textArea.selectionStart){ //FF
      textArea.selectedText = textArea.substring(textArea.selectionStart,textArea.selectionEnd);
   }
  
   alert("Selection Start==> " + textArea.selectionStart + "\n" +
      "Selection End  ==> " + textArea.selectionEnd + "\n" +
      "Selected Text  ==> " + textArea.selectedText + "\n" +
      "TextArea Value ==> " + textArea.value);
 }

HTML:

<textarea id="textarea1"></textarea> <button onclick="getTextAreaSelection()">Get Selection Info</button>