4

I am trying to use javascript to apply a certain style to the pages based on which browser the user is using. I can detect all of the browsers except for IE/Edge. In my code snippet I am just trying to detect IE/Edge and apply the style.

Here is my code:

var bodyStyle = document.querySelector("#bodyArea");
if((navigator.userAgent.indexOf("Edge") != -1 ) || (!!document.documentMode == true ))
{
    alert("asdf");
    bodyStyle.style.paddingTop = "500px";
}
else
{
    bodyStyle.style.paddingTop = "300px";
}

When I put an alert in the else section it gives me an alert, but it doesn't work on the if part. So I think my problem is occurring when I try to detect IE/Edge. Or if it lay elsewhere, let me know. If anyone has any feedback, it will be greatly appreciated. Thanks in advance!

5
  • 3
    Sorry. Have to ask... Why are you trying to revise the page just for IE & Edge users? Commented Nov 30, 2015 at 1:34
  • 2
    Please use css for this! Your life will change: stackoverflow.com/questions/32940965/… Commented Nov 30, 2015 at 1:35
  • @JonathanLonowski I am doing so because I applied for an internship, and I am trying to make everything function/appear correctly across all the browsers, but IE/Edge won't work with any of my code:'( If it was just for my own use or whatever, I wouldn't really care about IE/Edge users haha Commented Nov 30, 2015 at 1:40
  • 2
    @Austin If the differences are drastic, check that the browser is using standards mode vs. a legacy document mode when rendering the page. If it's an intranet site, IE/Edge may default to a compatibility view. The use of Enterprise Mode can also set this. Commented Nov 30, 2015 at 1:46
  • 2
    Possible duplicate of How do I detect IE and Edge browser? Commented Feb 25, 2016 at 15:27

1 Answer 1

2

You can use this custom script to detect IE/Edge:

if (/MSIE 10/i.test(navigator.userAgent)) {
   // this is internet explorer 10
   window.alert('isIE10');
}

if(/MSIE 9/i.test(navigator.userAgent) || /rv:11.0/i.test(navigator.userAgent)){
    // this is internet explorer 9 and 11
    window.location = 'pages/core/ie.htm';
}

if (/Edge\/12./i.test(navigator.userAgent)){
   // this is Microsoft Edge
   window.alert('Microsoft Edge');
}

Check out this page for the latest IE and Edge user agent strings: https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx

Sign up to request clarification or add additional context in comments.

13 Comments

I believe this will not work when the user agent is spoofed but should be fine in general.
IMO having to detect the browser in JS means that their is a bigger problem somewhere. In OP's use case, there is absolutely no need of client script detection of the browser. Presentation should always be worked in presentation code, not in JS.
Do you have your script file correctly called in your HTML file?
I'll keep my eyes open and see if I can find a solution as well.
@Gothburz I found this. If it can be of any help. social.technet.microsoft.com/Forums/ie/en-US/…. It seems that Edge is treating external js differently.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.