Detect and Change User Agent Via Javascript

R

Rainmanjam

I have a web site that uses MSN Live Maps. It works in Opera if I
change the User Agent to IE. Is there a way to change the User Agent
via Javascript for one page?
 
R

RobG

I have a web site that uses MSN Live Maps. It works in Opera if I
change the User Agent to IE. Is there a way to change the User Agent
via Javascript for one page?

Find whatever variable is used to store the user agent (usually
something like var IE = true/false) and just set it to true. But I
expect that will break much more than it fixes.

From the little I have seen of MS Live Maps, I wouldn't use it for a
web site. It uses extensive browser sniffing and requires the
download of a 750kB script file. In another thread, I did a search
for the string "attachEvent" and got 234 hits whereas addEventListener
only rated 11 - seems to me it is deliberately designed to provide
more features for IE than any other browser.

Now that is fine, MS are quite at liberty to create IE-specific web
libraries, but if you want a site that works well in other browsers,
don't use it.
 
R

Rainmanjam

Find whatever variable is used to store the user agent (usually
something like var IE = true/false) and just set it to true. But I
expect that will break much more than it fixes.

From the little I have seen of MS Live Maps, I wouldn't use it for a
web site. It uses extensive browser sniffing and requires the
download of a 750kB script file. In another thread, I did a search
for the string "attachEvent" and got 234 hits whereas addEventListener
only rated 11 - seems to me it is deliberately designed to provide
more features for IE than any other browser.

Now that is fine, MS are quite at liberty to create IE-specific web
libraries, but if you want a site that works well in other browsers,
don't use it.

I understand what you are saying but MS live maps is a requirement of
a project I am doing. I figured it out and posting results:
if (browser.indexOf("Opera") >= 0 )

{

navigator.userAgent="Mozilla/5.0 (compatible; MSIE 7.0; Windows NT
5.2)";

}

Easy Peazy
 
T

Thomas 'PointedEars' Lahn

Rainmanjam said:
[MS Live Maps is not suitable for the Web]

I understand what you are saying but MS live maps is a requirement of
a project I am doing. I figured it out and posting results:
if (browser.indexOf("Opera") >= 0 )

{

navigator.userAgent="Mozilla/5.0 (compatible; MSIE 7.0; Windows NT
5.2)";

}

Easy Peazy

Hardly. Assignment to navigator.userAgent might not even be possible,
throwing an exception.

Please trim your quotes.


PointedEars
 
M

My Pet Programmer

Rainmanjam said:
I understand what you are saying but MS live maps is a requirement of
a project I am doing. I figured it out and posting results:
if (browser.indexOf("Opera") >= 0 )

Are you testing to make sure "Opera" is in the string here? Yes. But
you're not testing for the false value String.indexOf will give you back
reliably, which is -1. That line should really be:

if (browser.indexOf("Opera") != -1 )

In this case it doesn't hurt you, but it's a good practice, and will
make your code more readable, IMHO.

As var as the code itself:

<script type="text/javascript">

var browser = navigator.userAgent;

if (browser.indexOf("Opera") >= 0 )
{
navigator.userAgent="Mozilla/5.0 (compatible; MSIE 7.0; Windows NT5.2)";

}

document.write (navigator.userAgent);
// Firefox writes:
Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.11)
Gecko/20071127 Firefox/2.0.0.11

// IE7 writes:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Comcast Install 1.0;
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC1; .NET
CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)

// Safari on Win writes:
Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/523.13
(KHTML, like Gecko) Version/3.0 Safari/523.13
</script>

So it appears to me from the actual tests that you can't modify it. This
works for you? I would be careful with the assumption that it will
continue to do so. IMHO, of course.

~A!
 
R

Rainmanjam

Rainmanjam said:


Are you testing to make sure "Opera" is in the string here? Yes. But
you're not testing for the false value String.indexOf will give you back
reliably, which is -1. That line should really be:

if (browser.indexOf("Opera") != -1 )

In this case it doesn't hurt you, but it's a good practice, and will
make your code more readable, IMHO.

As var as the code itself:

<script type="text/javascript">

var browser = navigator.userAgent;

if (browser.indexOf("Opera") >= 0 )
{
navigator.userAgent="Mozilla/5.0 (compatible; MSIE 7.0; Windows NT5.2)";

}

document.write (navigator.userAgent);// Firefox writes:

Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.11)
Gecko/20071127 Firefox/2.0.0.11

// IE7 writes:

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Comcast Install 1.0;
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC1; .NET
CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)

// Safari on Win writes:
Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/523.13
(KHTML, like Gecko) Version/3.0 Safari/523.13
</script>

So it appears to me from the actual tests that you can't modify it. This
works for you? I would be careful with the assumption that it will
continue to do so. IMHO, of course.

~A!

Interesting. I plugged the above javascript on my site http://njection.com/speedtrap
and it works just fine. Tried it on several versions of Opera.
 
R

Rainmanjam

Interesting. I plugged the above javascript on my sitehttp://njection.com/speedtrap
and it works just fine. Tried it on several versions of Opera.

In addition, I believe the user agent is being followed in the
Javascript.. not the browser. With the "document.write
(navigator.userAgent); " command, you are asking the browser to
directly report its agent. In its use with MS Live Maps, the user
agent is followed in the Javascript and that is how it's working. I
could just be blowing smoke.
 
D

David Mark

[snip]
Interesting. I plugged the above javascript on my sitehttp://njection.com/speedtrap

Not really.
and it works just fine. Tried it on several versions of Opera.

Oddly enough, that browser wasn't among the test cases.

So it works on Opera and virtually nothing else (and may throw an
exception in some agents.) Regardless, it's a completely ridiculous
thing to do.

BTW, some versions of Opera allow the user to change the userAgent
string to eliminate the "Opera" portion. This is one of many reasons
that browser sniffing cannot be relied upon.
 
R

Rainmanjam

[snip]


Interesting. I plugged the above javascript on my sitehttp://njection.com/speedtrap

Not really.
and it works just fine. Tried it on several versions of Opera.

Oddly enough, that browser wasn't among the test cases.

So it works on Opera and virtually nothing else (and may throw an
exception in some agents.) Regardless, it's a completely ridiculous
thing to do.

BTW, some versions of Opera allow the user to change the userAgent
string to eliminate the "Opera" portion. This is one of many reasons
that browser sniffing cannot be relied upon.

I totally understand. This case was just with Opera and getting it
working with MS Live Maps. Thank you for your feedback.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top