User Agents.

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

User Agents.

Post by JellyFish »

Hey, what does a user agent string have in it? I know it's a string from a web browser and other place, mainly the user.

But what's up with it? What is it made up of? I need to know more about this string in order to check it for validity.

So if anyone have any information on the user agent string that explains it, in a straightforward fashion. Or if you would like to explain it, I'd so much appreciate all the help I could get. :D

Thanks!
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Re: User Agents.

Post by neophyte »

Its a string that's passed in your http request that supposedly defines what what kind of browser you are connecting with. Unfortunately they can't be trusted. They can be altered by users. They can be altered on every request. If you want to observe them you should install the "live headers" extension for Firefox. Then you can watch them as they requests cycle. There's also a Firefox extension called Modify Headers. Through which you could modify your User-Agent header to be whatever you want.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: User Agents.

Post by JellyFish »

neophyte wrote:Its a string that's passed in your http request that supposedly defines what what kind of browser you are connecting with. Unfortunately they can't be trusted. They can be altered by users. They can be altered on every request. If you want to observe them you should install the "live headers" extension for Firefox. Then you can watch them as they requests cycle. There's also a Firefox extension called Modify Headers. Through which you could modify your User-Agent header to be whatever you want.
I see. So how might I go about serving different scripts to different browser, server-side?
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Re: User Agents.

Post by Josh1billion »

JellyFish wrote:I see. So how might I go about serving different scripts to different browser, server-side?
You have to figure out which browser the user is using (see: link), and then print() out a different script depending on the browser.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

This reeks of bad design - why are you serving different js depending on the browser?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Kieran Huggins wrote:This reeks of bad design - why are you serving different js depending on the browser?
Especially when you can spoof them.

By changing your about:config in Firefox, or this handy addon.

https://addons.mozilla.org/en-US/firefox/addon/59
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

There could be a good reason for what he's trying to do. I could see it if you need to use a hack to make a script compatible with a certain browser. In that case, though spoofing browsers would be possible, it'd also be very pointless.. :P
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I posted a class I had hacked to offer a clean output of User Agent data. I know it is around here somewhere. Some searching might help you find it.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

I'm constructing a server-side architecture, if you will, so that I could easily set variables such as $headerArgs['cssImports'] then include my header.php script which echos those variables.

My newest members of the array will be something like: $headerArgs['IECssImports'] or headerArgs['IE6JsImports']. This gives me the ability to add script and stylesheet imports without having to change my global header.php script.

Hopefully that make sense. But what I need to know now is: How do I serve different variables to different browser on the server's side? HTTP_USER_AGENT is the only server-side solution that I know of, at them moment.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

if you're just making up for a few IE bugs, you can always use IE's conditional comments:

Code: Select all

<head>
   <!--[if IE 5]>
      <script src="/script/ie5.js"></script>
      <link rel="stylesheet" href="/css/ie5.css" />
   <![endif]-->
</head>
I would rethink your solution if you're trying to do anything more complicated than this. jQuery includes native cross-browser compatibility!
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

This is what I was thinking of doing. But I would like my scripts to be flexible enough to import css code for all IE5.5+ browsers. In order to be extremely flexible with IECCSS (IE Conditional Comment Style Sheets) I would have to write this:

Code: Select all

<!--[if IE]-->
<?php echo $headerArgs['IEImports']; ?>
<![end if]-->

<!--[if IE 7]-->
<?php echo $headerArgs['IE7Imports']; ?>
<![end if]-->

<!--[if IE 6]-->
<?php echo $headerArgs['IE6Imports']; ?>
<![end if]-->

<!--[if IE 5.5]-->
<?php echo $headerArgs['IE5.5Imports']; ?>
<![end if]-->

<!--[if lt IE 5.5]-->
<?php echo $headerArgs['IE55LtImports']; ?>
<![end if]-->

<!-- etc... Or something like this. -->
I was thinking that maybe there is a much better solution for this then IE Comments; a php solution maybe.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Everah wrote:I found the code I had put up before.
Hey that looks pretty cool. Hm.. maybe even better if you could add PSP, Wii, and mobile support. ;) I don't know much about mobile web browsing, but the PSP thing would be useful since you could format a page to fit the PSP's screen better if you knew the user was browsing via PSP.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

All that class really does is take what is presented by the user agent and parses into something more useable. If the UA presents itself as a Wii or iPhone or anything else, the class will be able to tell and report it.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I can't help but feel like you're approaching this from the wrong angle. For the most part the Javascript/CSS will be the same in each browser. In Javascript specifically, you should be testing not for platforms but instead for method availability:

Code: Select all

if(methodname){
   v = methodname(data);
}else{
   v = workaround();
}
jQuery supports cross browser scripting out of the box, as do some other frameworks. Use them!

As for Wii or PSP support: if they're really important target platforms you should consider writing a custom view for each. Take a look at http://iphone.facebook.com for inspiration.
Post Reply