Friday, 21 October 2016

Java Script Code to detect browser and apply CSS property

How to detect browser using Java Script Code and apply CSS property ?

As we all know that IE browser behaves unexpected in some cases and need some extra CSS property or change in CSS property to work/view perfectly.

Below is the code to detect browser and if browser is IE then apply or override some css property.

<script type="text/javascript">  if ((navigator.userAgent.indexOf("MSIE") != -1) || (!!document.documentMode == true)) //IF IE > 10
         {

             $('.mycssclass').attr('style', "margin:20px !important");
         }

         else {

         }

</script>


In above code if browser is IE then in HTML wherever 'mycssclass' will be found it will add/override margin property to 20px.

No comments:

Post a Comment

Dear Readers, Please post your valuable feedback in the comment section if you like this blog or if you have any suggestions. I would love to hear the same from you. Thanks