Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Browser Detect - Beginner's lack of know-how

Status
Not open for further replies.

m9d

Technical User
Jan 6, 2005
13
0
0
US
Please forgive me if this is a recurring theme or nightmare, but I searched the forum and couldn't find an answer so I brought up a new topic.

Problem: I need to detect user's browser so I can display a "bookmark this page" link that works in IE and displays a browser specific message to Mac + PC users.

I have most PC browsers working correctly except Opera, so I would greatly appreciate it if you could take a look at what I am doing and reply with a fix.

Code:

function bookmarkThisPage(){
// copyright Brite Inc.

var browser=navigator.userAgent.toLowerCase();
var os=navigator.platform.toLowerCase();
var opera=(window.opera ? true : false);

if ((browser.indexOf("opera") != -1) && (opera("true"))) {
if (os.indexOf("win") != -1) {
window.alert('Your browser PC+OPERA does not support this function. Please click OK then press CTRL+T on your keyboard to bookmark this page.');
}
if (os.indexOf("mac") != -1) {
window.alert('Your browser MAC+OPERA does not support this function. Please click OK then press APPLE+T on your keyboard to bookmark this page.');
}
}

if (browser.indexOf("msie") != -1) {
if (os.indexOf("win") != -1) {
window.external.AddFavorite(self.location.href,document.title)
}
if (os.indexOf("mac") != -1) {
window.external.AddFavorite(self.location.href,document.title)
}
}

if (browser.indexOf("netscape") != -1) {
if (os.indexOf("win") != -1) {
window.alert('Your browser PC+NETSCAPE does not support this function. Please click OK then press CTRL+D on your keyboard to bookmark this page.');
}
if (os.indexOf("mac") != -1) {
window.alert('Your browser MAC+NETSCAPE does not support this function. Please click OK then press APPLE+D on your keyboard to bookmark this page.');
}
}

if (browser.indexOf("firefox") != -1) {
if (os.indexOf("win") != -1) {
window.alert('Your browser PC+FIREFOX does not support this function. Please click OK then press CTRL+D on your keyboard to bookmark this page.');
}
if (os.indexOf("mac") != -1) {
window.alert('Your browser MAC+FIREFOX does not support this function. Please click OK then press APPLE+D on your keyboard to bookmark this page.');
}
}

if (browser.indexOf("safari") != -1) {
if (os.indexOf("win") != -1) {
window.alert('Your browser PC+SAFARI does not support this function. Please click OK then press CTRL+D on your keyboard to bookmark this page.');
}
if (os.indexOf("mac") != -1) {
window.alert('Your browser MAC+SAFARI does not support this function. Please click OK then press APPLE+D on your keyboard to bookmark this page.');
}
}
}
 
what is this?
Code:
if ((browser.indexOf("opera") != -1) && [highlight](opera("true"))[/highlight]) {

do you have some function called "opera"? if not this will throw an error.

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Jeff-

Like I said in my post's title, I am beginner and I know enough to be considered dangerous. The code I posted is everything I have. I have been cutting and pasting different examples I am finding because most examples either don't work, or do more than I need them to.

The problem with Opera was that userAgent wasn't working, so I added (window.opera ? true : false), but I must not being referencing correctly.
 
well, unless the opera browser puts some property named "opera" into the window object, this code
Code:
 (window.opera ? true : false);
will always evaluate to false.

this code is a syntax error unless you have defined a function called "opera":
Code:
opera("true")

what i'm guessing you were after is this:
Code:
if ((browser.indexOf("opera") != -1) && (opera == true)) {
run this bit of code in opera and perhaps you'll find some useful property to sniff the opera browser:
Code:
<script type="text/javascript">
for (prop in navigator) 
 alert(prop + "=" + navigator[prop]);
</script>

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Jeff-
Thanks for the fix, I am going to test it. I did get it to detect Opera by using:
if ((browser.indexOf("opera") != -1) && (browser.indexOf("msie") != -1))
I found a piece of code where the author suggest testing for both Opera and MSIE userAgents. You can then also test for Opera by itself if the user has their prefences set to Opera, and Opera and Netscape if the user has it set to Mozilla.

So know I need to bug fix Mac+MSIE, that isn't working either.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top