1

Hi I've identified the HTML5/CSS3 elements that cause older browsers to struggle with my site. I'm aware of the opposition to using this method to redirect but I think it'll work for me fine. Tried this and a few others from forums and I'm testing using IE8.0.6001.18702 and it's not redirecting

<script type="text/javascript"> 
<!-- 
browser = navigator.appName 
ver = navigator.appVersion 
version = ver.substring(0,1) 
if (browser=="Internet Explorer") { 
if (version<="8.1") 
document.location.href="lores.htm" 
} 
if (browser=="Safari") { 
if (version<="6.0") 
document.location.href="lores.htm" 
} 
if (browser=="Firefox") { 
if (version<="5.0") 
document.location.href="lores.htm" 
} 
if (browser=="Chrome") { 
if (version<="15.0") 
document.location.href="lores.htm" 
} 
if (browser=="Opera") { 
if (version<="11.10") 
document.location.href="lores.htm" 
} 
//--> 
</script>

So the thing is nothing seems to work so I could really do with some advice. I've included this example to show the versions I want to redirect to 'lores.htm' for the basic website.

Any chance of a hand?

nobody
  • 19,814
  • 17
  • 56
  • 77
user3337851
  • 11
  • 1
  • 3
  • You can drop the `` around the Javascript. The reason to do this died with browsers like Netscape Navigator 2 decades ago. – Ingo Bürk Apr 14 '14 at 19:17

2 Answers2

1

As you pointed out, you really shouldn't be doing this. But if you're set on this, note that your condition checks are off:

navigator.appName resolves to "Microsoft Internet Explorer", not "Internet Explorer" like you have written.

Also, the first character navigator.appVersion will not provide you with the version of the browser. In IE 10, it resolves to "5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0;"

To make your code work, you need to update it to something like:

function get_browser_version(){
  var N=navigator.appName, ua=navigator.userAgent, tem;
  var M=ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
  if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
  M=M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];
  return M[1];
}

var browser = navigator.appName;
var version = get_browser_version();

if (browser=="Microsoft Internet Explorer") { 
    if (version<="8.1") 
        document.location.href="lores.htm" 
} 

Function copied from: How can you detect the version of a browser?

Please reconsider doing this. Are you having difficulty styling HTML5 elements? Consider using the html5shiv: https://code.google.com/p/html5shiv/

Community
  • 1
  • 1
Mister Epic
  • 16,295
  • 13
  • 76
  • 147
-3

Might you be missing the semi-colon's (EDIT: Not required but most parsers work better with them)?

Also, check out the version string, you're taking just the first character of the string you call 'ver' where you're comparing it to a string that's larger than that (look at opera '11.10'). You should substring at the first space (which should be after the full version number).

<script type="text/javascript">
browser = navigator.appName;
ver = navigator.appVersion;
version = ver.substring(0, ver.indexOf(' '));
if (browser == "Internet Explorer") {
    if (version <= "8.1")
        document.location.href = "lores.htm";
}
if (browser == "Safari") {
    if (version <= "6.0")
        document.location.href = "lores.htm";
}
if (browser == "Firefox") {
    if (version <= "5.0")
        document.location.href = "lores.htm";
}
if (browser == "Chrome") {
    if (version <= "15.0")
        document.location.href = "lores.htm";
}
if (browser == "Opera") {
    if (version <= "11.10")
        document.location.href = "lores.htm";
}
</script>
user3507600
  • 1,075
  • 9
  • 15
  • Semicolons in Javascript are optional (though generally recommended). – Ingo Bürk Apr 14 '14 at 19:17
  • Oh god I'm so sorry for the rookie errors guys, I'll update it give me a mo – user3337851 Apr 14 '14 at 19:19
  • @IngoBürk Actually, according to [this](http://www.w3schools.com/js/js_syntax.asp), that's not true. – user3507600 Apr 14 '14 at 19:19
  • @user3507600 Actually, you should [stay away from w3schools](http://www.w3fools.com), but even they get it right and cleary state on the page that it's optional to end statements with semicolons. – Ingo Bürk Apr 14 '14 at 19:22
  • Thank you so much, nailed it with: – user3337851 Apr 14 '14 at 19:23
  • @user3337851, you may want to adjust your assignment of your variable 'version' as I mentioned in my edit. – user3507600 Apr 14 '14 at 19:28
  • @IngoBürk, while you may technically be correct, I find that most JS parsers prefer (and function as intended) when they are not omitted (as is evidence by my solution fixing the problem). – user3507600 Apr 14 '14 at 19:32
  • @user3507600 As I said in the beginning: yes, it is good practice. But it's syntactically correct (unfortunately; I wish it wasn't!). – Ingo Bürk Apr 14 '14 at 19:36
  • @IngoBürk optional implies it would be parsed the same with or with out semi-colons, which clearly is not true; some parsers expect semi-colons. I'm pretty sure all of the semi-colons I added could be safely omitted according to [these rules](http://inimino.org/~inimino/blog/javascript_semicolons). – user3507600 Apr 14 '14 at 19:41
  • @user3507600 I'm not sure what you're trying to tell me. You say they were necessary in the first sentence, then you claim they can be omitted. Anyway. Any parser that requires semicolons to work does not implement the Javascript spec correctly. Simple as that. – Ingo Bürk Apr 14 '14 at 19:51
  • @IngoBürk, I'm trying to tell you that you were right that the spec does not require them. But due the lack of compliance by some parsers, it is required by necessity. – user3507600 Apr 14 '14 at 19:54
  • @user3507600 I'm not aware of any Javascript engine (used in any major browser) that fails if semicolons are omitted. Which one are you referring to? – Ingo Bürk Apr 14 '14 at 20:04