Welcome to WebmasterWorld Guest from 54.158.166.6

Forum Moderators: open

Message Too Old, No Replies

javascript redirect for just one browser + platform combination

browser sniffer for a specialized case

   
1:06 am on Jan 19, 2003 (gmt 0)

10+ Year Member



Hi everyone,

I need a javascript that would allow when my page is loaded to automatically redirect all & only Mac users using IE5 to a different page. All other users would need to stay on the page they just loaded. Can someone help me out or point me towards a tutorial/script to do this?

3:25 am on Jan 19, 2003 (gmt 0)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



This ought to do it.

1. First, I create a variable "agt", which sets the userAgent string to lower case.

2. The next line finds the starting position of the string "m-a-c" in the agent string. If that starting position is not equal to -1 (in other words, if the string is actually there) then the variable "is_mac" is set to a Boolean (true or false) value of "true".

3. Next, set another variable called "version" to be just the integer part of the navigator.appVersion string. This will truncate the decimal part of the version number.

4. Look to see if the string "m-s-i-e" appears in in the agt string (similar to step 2). If it's there (again, not equal to -1), then is_ie is set to "true".

5. Set a new variable to combine the "true or false" value of "is_ie" and the statement "version = 5". This gives us the "true or false" value for is_ie5. According tom Boolean operation, "is-ie5" will be true only if BOTH values are true.

6. The final step. If both "is_mac" and "is_ie5" are true, then replace the current location. I used the replace() method so that the user's Back Button will stay intuitive.

var agt=navigator.userAgent.toLowerCase();
var is_mac = (agt.indexOf("mac")!=-1);
var version = parseInt(navigator.appVersion);
var is_ie = (agt.indexOf("msie")!= -1);
var is_ie5 = (is_ie && (version = 5));

if (is_mac && is_ie5) {location.replace("alternate_page.html")}

3:02 pm on Jan 19, 2003 (gmt 0)

10+ Year Member



thanks for the detailed explanation and please excuse my ignorance in this subject but how do I include this to my page?

Do I just put this in my head tag?

<script language=JavaScript>
var agt=navigator.userAgent.toLowerCase();
var is_mac = (agt.indexOf("mac")!=-1);
var version = parseInt(navigator.appVersion);
var is_ie = (agt.indexOf("msie")!= -1);
var is_ie5 = (is_ie && (version = 5));

if (is_mac && is_ie5) {location.replace("alternate_page.html")}
</script>

4:27 pm on Jan 20, 2003 (gmt 0)

10+ Year Member



this worked for me, in case this can help anyone else out there in the future;

<script language="JavaScript">
if(navigator.appVersion.indexOf("MSIE 5.")!=(-1)
&& navigator.appVersion.indexOf("Macintosh")!=(-1)) {
top.location.href = "new-iframe-page.html";
}
</script>

Good luck.

5:03 pm on Jan 20, 2003 (gmt 0)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yes, it can be condensed. The code I wrote was broken into bite sized pieces for easy understanding and future editing - hoping to help other people in similar but not identical situations.

kelly001, your code works, but it "breaks" the Back Button for your visitor. I'd suggest changing this line:

top.location.href = "new-iframe-page.html";

to read like this:

top.location.replace("new-iframe-page.html");

Glad you've got your solution.

3:27 am on Jan 21, 2003 (gmt 0)

10+ Year Member



thank you very much, I hadn't noticed that... your little fix works wonders in that regards! thanks