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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to recognize the browser in HTML code

Status
Not open for further replies.

nomy

Programmer
Jul 20, 1999
23
0
0
PK
Any one pl. tell me about detecting the browser type using HTML. or Pl suggest other method
 
You can do this with Vbscript(pointless though because unless the browser is IE, Vbscript wont work to even detect the browser) or JavaScript, I cant think of the function off the top of my head, but I know javascript would be the way to go. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Dear Nomi,<br><br>Ok this gets tough trying to describe in text, but here goes.<br><br>Using client side script is a big catch22. If the browser doesn't support scripting... then what? So if your script is running at all then you know that much.<br><br>Then IE 4+ browsers have a 'window.clientInformation' object that has several interesting pieces of information. So if you can get that object you already know you have an IE 4 or above browser so you may not care about the rest of the information in the object!<br><br>Most of the available client scripts on the net will do things like this:<br><br>if ( document.Layers)<br>&nbsp;&nbsp;var Nutscrap4 = true;<br>else if ( document.all)<br>&nbsp;&nbsp;var IEwhatever = true;<br><br>In other words the script looks for certain objects that the script want's to use to do it's job. Very crude, but it actually works... well if the browser is even executing the script that is!<br><br>What a freakin mess uh? So the simplest most attractive technique is to use server side detection. The browser may send a 'USER_AGENT' string to the web server which is available to your server side code whatever your platform is. Also there are 3rd party products that you might be interested in, most notably one called 'Browser Hawk'.<br><br>&quot;But, that's just my opinion... I could be wrong&quot;.<br>-pete<br>
 
well it is also a good idea to put <br>&lt;Script...&gt;<br>&lt;!--<br>Script here<br>--&gt;<br>&lt;/Script&gt;<br><br>if the browser doesnt support scripts, at least the script code will not be shown on the screen if the browser supports standard HTML comments. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Karl is absolutely correct of course. I apologize for the incomplete post... I guess I thought that qualified as a &quot;duh&quot;. I will try to be more diligent in the future.<br><br>-pete
 
JavaScript is essentially an extention of Java so in theory it will run on any system with the Java Virtual Machine installed.&nbsp;&nbsp;I haven't tested this to find out since I don't have any browsers that don't support scripting.&nbsp;&nbsp;Anyways, here is the best code around for doing it.<br><br><br>/*<br>function Is() {<br>&nbsp;&nbsp;&nbsp;&nbsp;var agent = navigator.userAgent.toLowerCase();<br>&nbsp;&nbsp;&nbsp;&nbsp;this.major = parseInt(navigator.appVersion);<br>&nbsp;&nbsp;&nbsp;&nbsp;this.minor = parseFloat(navigator.appVersion);<br>&nbsp;&nbsp;&nbsp;&nbsp;this.ns&nbsp;&nbsp;= ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));<br>&nbsp;&nbsp;&nbsp;&nbsp;this.ns2 = (this.ns && (this.major == 2));<br>&nbsp;&nbsp;&nbsp;&nbsp;this.ns3 = (this.ns && (this.major == 3));<br>&nbsp;&nbsp;&nbsp;&nbsp;this.ns4b = (this.ns && (this.minor &lt; 4.04));<br>&nbsp;&nbsp;&nbsp;&nbsp;this.ns4 = (this.ns && (this.major &gt;= 4));<br>&nbsp;&nbsp;&nbsp;&nbsp;this.ie&nbsp;&nbsp;&nbsp;= (agent.indexOf(&quot;msie&quot;) != -1);<br>&nbsp;&nbsp;&nbsp;&nbsp;this.ie3&nbsp;&nbsp;= (this.ie && (this.major == 2));<br>&nbsp;&nbsp;&nbsp;&nbsp;this.ie4&nbsp;&nbsp;= (this.ie && (this.major &gt;= 4));<br>&nbsp;&nbsp;&nbsp;&nbsp;this.op3 = (agent.indexOf(&quot;opera&quot;) != -1);<br>}<br><br>var is = new Is();<br>*/<br><br>Then just use if statments to do what you want<br><br>/*<br>if(is.ns4){<br>&nbsp;&nbsp;&nbsp;&nbsp;code...<br>}<br>*/<br>etc <p>JMJimmy<br><a href=mailto: > </a><br><a href= > </a><br><br>
Know (ish) VB, Java, Javascript, HTML, DHTML, VBScript, Qbasic.
 
Dear JMJimmy,<br><br>You stated:<br><br>&gt;JavaScript is essentially an extention of Java so in theory <br>&gt;it will run on any system with the Java Virtual Machine installed.<br><br>I don't believe I can agree with your assessment of this issue. You might want to look into this a little more.<br><br>-pete
 
palbano I know it is &quot;duh&quot; to me as well, but you'd be surprised how many people I see that dont put comments in the script tags. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
For the sake of completeness:<br>&lt;SCRIPT language ='Javascript'&gt;<br>&lt;!-- hide from browsers not supporting Javascript<br>function somefunction() (<br>}<br>//--&gt;<br>&lt;/SCRIPT&gt;<br>&lt;NOSCRIPT&gt;<br>Your browser does not support Javascript. Please enable Javascript or upgrade to a newer browser.<br>&lt;/NOSCRIPT&gt;<br><br>This will show a friendly message to the user if their browser doesn't support Javascript, rather than showing a page that appears not to work.<br> <p>nick bulka<br><a href=mailto: > </a><br><a href= > </a><br>
 
I found a way to run through it but it is kind of messy and I havent tested it very well but the idea is sound.<br><br>&lt;script language=&quot;javascript1.1&quot;&gt; <br>&nbsp;&nbsp;&nbsp;&nbsp;this makes this section a &quot;script&quot; even to browsers with javascript turned off.<br><br>&lt;!-- hide from old browsers so the code isn't printed<br>/*--&gt; hide from javascript enabled browsers but show to old browsers<br><br>&lt;body&gt;<br>Welcome to my site, &lt;a href=&quot;content.htm&quot;&gt;Come on in...&lt;/a&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br>**this should end the line for old browsers**<br><br>&lt;!-- */ rehide for old browsers just in case...<br><br>put favorate browser selection javascript code here..<br><br>//--&gt;<br>&lt;/script&gt;<br>&lt;noscript&gt;<br><br>lastly this is for javascript browsers with javascript turned off.<br><br>&lt;/noscript&gt;<br><br>--&gt; this just ties up the &lt;!-- started a few lines up, probably don't need it.<br><br><br>Naz
 
wow, that's a bit much. First, about JavaScript being an extension of Java- like palbano said, that's not quite correct. Sun Microsystems created Java; they had named it Oak at first (Java wasn't even the original name) after the tree outside their office, but changed it to Java after learning that there was already a programming language called Oak. Java is a <b>programming</b> language.<br><br>JavaScript is a <b>scripting</b> language created by Netscape- it wasn't named JavaScript when it first came out either, but rather &quot;LiveScript&quot; (to work with their LiveWire server software). Sun and Netscape got together to create a scripting language that would eventually be called JavaScript, but it's only remotely like Java. A lot of the syntax is similar, but they differ greatly, if for no other reason than that they serve two very different purposes. Java does (more or less) run anywhere- you only have to compile it once. JavaScript does not exactly meet up to this for one very important reason- different browsers support different versions and different models. Netscape might have started the whole thing, but they really slacked off towards the end of the race. IE's DOM support for IE5 vs. NN4 is far superior, and NN6 will not even support the NN4 stuff- in fact, the NN6 DOM looks a lot more like the IE5 DOM than the NN4 (if only because NN6 is a more standards-compliant browser, and IE5 was a lot closer to the standard than NN4 was).<br><br>So hence the need to create dHTML and JavaScript that works on both pages. For simple browser detection stuff, I'll go with the old browser.appName, but for dHTML I use document.all vs. document.layers . However, you can't do that anymore, as NN6 does not support layers anymore (thank God).<br><br>The best bet is to write code that works on both browsers without having to give in to proprietary code :eek:) but that's obviously more easier said than done... best of luck, and happy scripting. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
imotic dude, i dont understand your quote underneith your name thingy... Anyone care to elaborate?<br><br>Thanks. <p> Karl<br><a href=mailto:mc_karl@yahoo.com>mc_karl@yahoo.com</a><br><a href= > </a><br> ~ ~ ~ ~<br>
K A R L<br>
~ ~ ~ ~
 
the quotes supposed to make you think about it, anyways I feel that if you have access to server-side scripting, you can easily determine the browser in it's HTTP Request, since the browser sends a heck of alot of information about its self in the HTTP request, not just &quot;Get ....something.htm&quot;<br>the Request Header will have all the information, of course this post is rather useless to you if you can only work with client side scripting. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top