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!

  • 3
    Sorry. Have to ask... Why are you trying to revise the page just for IE & Edge users? – Jonathan Lonowski Nov 30 '15 at 01:34
  • 2
    Please use css for this! Your life will change: http://stackoverflow.com/questions/32940965/how-to-target-microsoft-edge-with-css – Frederik.L Nov 30 '15 at 01: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 –  Nov 30 '15 at 01:40
  • 2
    @Austin If the differences are drastic, check that the browser is using [standards mode](https://msdn.microsoft.com/en-us/library/cc288325.aspx) vs. a [legacy document mode](https://msdn.microsoft.com/en-us/library/jj676915.aspx) when rendering the page. If it's an intranet site, IE/Edge may default to a compatibility view. The use of [Enterprise Mode](https://technet.microsoft.com/en-us/library/mt270205.aspx) can also set this. – Jonathan Lonowski Nov 30 '15 at 01:46
  • 2
    Possible duplicate of [How do I detect IE and Edge browser?](http://stackoverflow.com/questions/33152523/how-do-i-detect-ie-and-edge-browser) – Paul Sweatte Feb 25 '16 at 15:27

1 Answers1

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

Peter Girnus
  • 2,673
  • 1
  • 19
  • 24
  • I believe this will not work when the user agent is spoofed but should be fine in general. – Iman K Nov 30 '15 at 01:34
  • Correct, op just wants to detect IE/edge with JS. – Peter Girnus Nov 30 '15 at 01:36
  • 3
    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. – Frederik.L Nov 30 '15 at 01:39
  • @Frederik.L I just came to that conclusion, about 5 mins ago, so I started to do some digging, and I think that my script file isn't linking to my html in IE/Edge or something, because it won't even give me a simple alert(), so I have no idea what is wrong at this point :| –  Nov 30 '15 at 01:52
  • 1
    Do you have your script file correctly called in your HTML file? – Peter Girnus Nov 30 '15 at 01:57
  • @Gothburz The script file will run in chrome and other browsers, but it isn't running in Edge, I tried using in the actual html file and it worked in Edge, it just isn't linking to my .js file for some reason. This is my code to link it `` –  Nov 30 '15 at 02:05
  • And your JS file is in the root directory and not in its own folder? – Peter Girnus Nov 30 '15 at 02:08
  • @Gothburz Yeah it is, all of my files for this site are just in the same folder. –  Nov 30 '15 at 02:09
  • http://stackoverflow.com/questions/32617995/unable-to-run-javascript-in-external-file-in-microsoft-edge-browser seems you're not the only one. – Peter Girnus Nov 30 '15 at 02:12
  • @Gothburz I just got done reading that post lol. Ughhh I am ready to send my project, but IE/Edge sucks!! Well thanks for your help! I appreciate it! I guess I will just send it. –  Nov 30 '15 at 02:15
  • 1
    I'll keep my eyes open and see if I can find a solution as well. – Peter Girnus Nov 30 '15 at 02:16
  • 1
    @Gothburz I found this. If it can be of any help. https://social.technet.microsoft.com/Forums/ie/en-US/769317b9-318c-40b2-ba4f-7a9d27265e71/microsoft-edge-sandboxing-of-external-javascript-code-breaks-prototypejs?forum=ieitprocurrentver. It seems that Edge is treating external js differently. – Frederik.L Nov 30 '15 at 02:22
  • @Frederik.L I was messing around with positioning of my script file call, It doesn't seem to work when I link to it in the it doesn't work, but I put the link at the bottom of the and now it works fine, it just breaks my html5 local storage that I have intact. So I am close –  Nov 30 '15 at 02:31