?
Solved

browser detection and SSI's

Posted on 1999-11-07
29
Medium Priority
?
519 Views
Last Modified: 2010-08-05
Ok, i think this should be fairly easy to answer for someone...i'm sure there is something I am just missing.  What i am trying to do is use ASP to detect the browser (PLEASE don't tell me to do it in Javascript, i don't want to).  I have the code for doing the detection which works just fine (i stole it from webmonkey).  here is the code.

<%

dim BrowserType

set bc = Server.CreateObject("MSWC.BrowserType")

if bc.browser="IE" then

 BrowserType = "MSIE"

elseif bc.browser="Netscape" then

 BrowserType = "Netscape"
 
elseif bc.browser="Lynx" then
 
 BrowserType = "Lynx"

end if

%>

<%

select case BrowserType  

case "Lynx"  

  Response.Write("You're using Lynx! Whatever!")    
 
case "MSIE"
 
   Response.Write("you are using IE")
 
case "Netscape"  
 
  Response.Write("You're using Netscape!")    
 
case else  
 
  Response.Write("You're using some other browser I don't know about.")
 
End select

%>




what i would like to do is instead of using Response.Write to write out what the user is using, is include another file for my navigation.  ex:  


<!--#include file="navie.inc"-->


I tried just replacing the line:

Response.Write("you are using IE") with the include line, this does not work, return errors.  i tried doing something similar to:

Response.Write("<!--#include file="navie.inc"-->")  

of course, errors.  I know what i am trying to do it doable, Hotbot uses something like this on their site.  Anyways, I would be much obliged if anyone can help.


BRUNO
0
Comment
Question by:bruno
  • 15
  • 9
  • 2
  • +2
29 Comments
 
LVL 2

Expert Comment

by:Pepster
ID: 2189951
Hi

Try this:

Load a variable with the type of browser and then include it. eg:

Instead of:
<%
blah
Response.Write("you are using IE")
blah
%>

<%
blah
strBrowserType = "IExplorer"
blah
%>

and then in your html near the top but below the above code:

<!--#include file="<%=strBrowserType%> & ".inc"-->

Hope this helps

0
 
LVL 2

Expert Comment

by:Pepster
ID: 2189953
Then use the file IExplorer.inc for whatever you want to do.
0
 

Expert Comment

by:amnh
ID: 2190263
I do not believe Pepster's comment will be effective because the way ASP handles server side includes.  All included files are loader prior to the script being run.

if the include file contains all of the HTML you want the end user to see then you could use the 'response.redirect' to send the browser specific page to the user. This way changes th URL seen by th end user.

To avoid changing the URL you can user the 'Scripting.FileSystemObject'  to open an text file and display its contents using the 'response.write' command.  Draw back with this solution is the file being opened can not contain ASP code.


0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 18

Author Comment

by:bruno
ID: 2190285
pepster,

i haven't had a chance to try your idea yet, i will in a bit.

amnh,

i don't think you understand what i am trying to do.  i am using an include file in my web page that contains the navigation for the page.  because my navigation doesn't work in netscape, i need to be able to include a different file depending on which browser it is.  i do not want to redirect anyone anywhere.  please re read the original question, and if you have any questions i will try to clear them up.

Thanks.

pepster, i'll post another comment soon letting you know how your idea worked out.  
if anyone is interested where i got this idea, check out webmonkey, look under programming, then asp, then the inrto to asp article, third page on browser detection.
0
 
LVL 18

Author Comment

by:bruno
ID: 2190343
pepster,

didn't seem to work...here is the code i am using as it stands...i'm testing in IE so that is the only one that i've changed just in cause you were wondering.


<%

dim BrowserType

set bc = Server.CreateObject("MSWC.BrowserType")

if bc.browser="IE" then

 BrowserType = "MSIE"

elseif bc.browser="Netscape" then

 BrowserType = "Netscape"
 
elseif bc.browser="Lynx" then
 
 BrowserType = "Lynx"

end if

%>

<%

select case BrowserType  

case "Lynx"  

  Response.Write("You're using Lynx! Whatever!")    
 
case "MSIE"
 
 strBrowserType = "navie"

 
case "Netscape"  
 
  Response.Write("You're using Netscape!")    
 
case else  
 
  Response.Write("You're using some other browser I don't know about.")
 
End select

%>


<!--#include file="<%=strBrowserType%> & ".inc"-->



________________________


and this is the error i get.

________________________


Active Server Pages error 'ASP 0126'

Include file not found

/bruno/nav1.asp, line 49

The include file '<%=strBrowserType%> & ' was not found.
0
 
LVL 18

Author Comment

by:bruno
ID: 2190354
i also tried this, but this just includes the navnet.inc because it's the last one listed...*sigh*



<%

dim BrowserType

set bc = Server.CreateObject("MSWC.BrowserType")

if bc.browser="IE" then

 BrowserType = "MSIE"

elseif bc.browser="Netscape" then

 BrowserType = "Netscape"
 
elseif bc.browser="Lynx" then
 
 BrowserType = "Lynx"

end if

%>

<%

if BrowserType = ("MSIE") then %>
      <!--#include file="navie.inc"-->
<% else %>
      <!--#include file="navnet.inc"-->
<% end if %>





0
 
LVL 4

Expert Comment

by:jmcvinney
ID: 2190911
This last one is the correct way to do it.  bear in mind that there are several versions of ie out there and they have different entries in the browscap.ini file which is the file that the ASP browser detection uses.  try response.write bc.browserType and see what it is putting out.  If you are on AOL it is not just IE.  to correct this use
if instr(bc.browserType, "IE" then
      'it's an  ie browser
....


     
0
 
LVL 18

Author Comment

by:bruno
ID: 2191611
jmcvinney,

what are you talking about????  the last one doesn't work, and i stated that.  i don't want it to tell me what browser they are using.  please re-read what i want done.

bruno
0
 

Expert Comment

by:amnh
ID: 2192060
brunobear,
Based on your comment to my answer, I am expanding what I described.  

<!-- #include file="navie.inc" --> are loaded before the script engine processes any ASP code.  Therefore doing conditional statements does not prevent the include files from being loaded.  These conditional statements may prevent some of the include files from being sent to the end users browser.

I use code similar to the following to load a single include file as a header, the conditional code has been removed.

<%
dim oFile
dim oFS
dim strhtml
dim strFileName
strFilename = "navie.inc"
set oFS = server.CreateObject("Scripting.FileSystemObject")
set oFile = oFS.OpenTextFile(strFileName)
strhtml = oFile.readall
Response.Write strhtml
oFS.close
set oFile = nothing
set oFS = nothing
%>

The file being included cannot contain ASP code as it is being sent directly to the brower.  In my case the file is the page header which contains nav and other information.  this file changed based on the user location, browser and authenication level.
Since the size of the header files was not small (5K) I did not want to load every possible file just to discard 95% of them.
0
 
LVL 18

Author Comment

by:bruno
ID: 2192146
amnh,

i appreciate the extra help.  i'm starting to get very frustrated.  I tried the code you gave, i got a Server.CreateObject error.  here is the code for my web page, maybe this will help.  at this point i'm not even opposed to using javascript if i have to.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
      <title>Untitled</title>
      
      
<LINK REL=StyleSheet HREF='style.css' TYPE='text/css' MEDIA=screen>      
<!--#include file="script.js"-->



</head>


<body background="images/background1.gif" BGCOLOR="#ffffff" onload="WM_initializeToolbar()">

<CENTER>
<TABLE CELLPADDING="2" CELLSPACING="2" BORDER="1" WIDTH="300">
  <TR>
    <TD WIDTH="300">

<font size=2 face=Verdana,Arial,Helvetica color=#000000>
<A HREF="main.asp" class="link">Home</A> | <A HREF="contact.asp" class="link">Contact</A> | <A HREF="sitemap.asp" class="link">Site Map</A>
</FONT>

    </TD>
  </TR>
 
</TABLE>
</CENTER>

<BLOCKQUOTE>

<TABLE BORDER="0" CELLPADDING="1" CELLSPACING="1" WIDTH="500" HEIGHT="350">
  <TR>
    <TD WIDTH="125" HEIGHT="325" VALIGN="TOP">

//----------------------------------------------------
//this is where i need to include the navigation file
//"navie.inc for IE
//navnet.inc for netscape and all others
//----------------------------------------------------


    </TD>
      <TD WIDTH="400" HEIGHT="325" VALIGN="TOP">
      
<!--#include file="maincontent.inc"-->

    </TD>
  </TR>
  <TR>
    <TD COLSPAN="2">
      
<!--#include file="footer.inc"-->

    </TD>
  </TR>
</TABLE>
</BLOCKQUOTE>


</body>
</html>







0
 
LVL 18

Author Comment

by:bruno
ID: 2192152
i'm increasing the points a bit too, i guess this is harder than i thought it would be.
0
 
LVL 9

Expert Comment

by:TTom
ID: 2192465
bb:

<%if BrowserType = ("MSIE") then %>
<!--#include file="navie.inc"-->
<% else %>
<!--#include file="navnet.inc"-->
<% end if %>

is an appropriate syntax to use to accomplish what you want.  The caveat is that all of the included files are loaded into memory.  Only the appropriate one will be output.

If this syntax is causing difficulty, something else is wrong.

Let us know exactly what is happening when you try this.

Tom



0
 
LVL 18

Author Comment

by:bruno
ID: 2192775
TTom,

Thanks for your input.  However, that didn't work.  What ends up happening is that navnet.inc is what's loading up because it's the second one listed.  I switched the code around to say:

<%if BrowserType = ("MSIE") then %>
<!--#include file="navnet.inc"-->
<% else %>
<!--#include file="navie.inc"-->
<% end if %>


and navie.inc loaded up.  for some reason it's just loading whichever one is placed last.  argh!  any more thoughts?

BRUNO
0
 
LVL 9

Expert Comment

by:TTom
ID: 2193863
Bruno:

I'm betting the problem is:

<% if BrowserType = ("MSIE") then%>

should be:

<% if BrowserType = "MSIE" then%>

Clearly, there is a problem with the "if" statement, as the loop is always evaluating to "else".

Tom
0
 
LVL 18

Author Comment

by:bruno
ID: 2194214
TTom,

True. However, taking away the ( ) didn't work.  What I'm wondering is, do the include statements need to be within the ASP code somehow?  Like:

<%
if BrowserType = "MSIE" then
<!--#include file="navie.inc"-->
 else
<!--#include file="navnet.inc"-->
end if
%>

Whenever I try this i get errors too, but it doesn't make sense to me (remember I'm a beginner) but it doesn't make sense how if they are NOT in between the <% %> tags how both of them won't get picked up.  i'm not giving up yet, i'm positive there is a way to do this.


BRUNO

0
 
LVL 9

Expert Comment

by:TTom
ID: 2194342
In short, when the files are outside the <%%> tags they get loaded into memory, but, in theory, only the appropriate one will be executed.

This code executed exactly as expected on my machine:

<%dim var%>
<%if var = "two" then%>
<!-- #include file="two.inc" -->
<%else%>
<!-- #include file="one.inc" -->
<%end if%>
<% var = "two"%>
<% if var = "two" then%>
<!-- #include file="two.inc" -->
<% else %>
<!-- #include file="one.inc" -->
<% end if %>

Need to do some more delving into what exactly you are trying to do and what results you are achieving.

If it is a matter that the appropriate file is not loading, I would very carefully examine the code which generates the variable.  Perhaps, generate a response.write immediately before your code for the include file or manually set the variable to the value you want and change it to be sure that the files load properly when the variable evaluates properly.

Will keep trying!

Tom
0
 
LVL 18

Author Comment

by:bruno
ID: 2195082
Tom,

THis code successfully returns the "navie.inc" file...

<% dim BrowserType %>

<% BrowserType = "MSIE" %>
<% if BrowserType="MSIE" then %>
<!--#include file="navie.inc"-->
<%else%>
<!--#include file="navnet.inc"-->
<%end if%>


however...this code returns returns "navnet.inc"...

<% dim BrowserType %>

<%
set bc = Server.CreateObject("MSWC.BrowserType")
if bc.browser="IE" then
 BrowserType = "MSIE"
elseif bc.browser="Netscape" then
 BrowserType = "Netscape"
elseif bc.browser="Lynx" then
 BrowserType = "Lynx"
end if
%>      


<% if BrowserType="MSIE" then %>
<!--#include file="navie.inc"-->
<%else%>
<!--#include file="navnet.inc"-->
<%end if%>


I don't get it!  if you look at the original question and the first code that i posted, that code works fine.  Maybe something to do with the "select case BrowserType" ??  

BRUNO
0
 
LVL 18

Author Comment

by:bruno
ID: 2195104
This code also returns "navnet.inc"...


<% dim BrowserType %>

<%
set bc = Server.CreateObject("MSWC.BrowserType")
if bc.browser="IE" then
 BrowserType = "MSIE"
elseif bc.browser="Netscape" then
 BrowserType = "Netscape"
elseif bc.browser="Lynx" then
 BrowserType = "Lynx"
end if
%>      

<% select case BrowserType %>      
<% case "MSIE" %>
   <!--#include file="navie.inc"-->  
<% case "Netscape" %>    
  <!--#include file="navnet.inc"-->        
<% case else %>    
  <!--#include file="navnet.inc"-->    
<% End select %>



BRUNO
0
 
LVL 9

Expert Comment

by:TTom
ID: 2195175
Now I am getting frustrated!!

<%
dim BrowserType
set Br = server.createobject("MSWC.BrowserType")
response.write Br.browser
if Br.browser = "IE" then
BrowserType = "MSIE"
end if
%>
<%if BrowserType = "MSIE" then%>
<!-- #include file="two.inc" -->
<%else%>
<!-- #include file="one.inc" -->
<%end if%>

Got it!!

Select case only seems to work with numeric values.  If you change your code thus, it should work:

<%
set bc = Server.CreateObject("MSWC.BrowserType")
if bc.browser="IE" then
 BrowserType = 1
elseif bc.browser="Netscape" then
 BrowserType = 2
elseif bc.browser="Lynx" then
 BrowserType = 3
end if
%>

<% select case BrowserType %>        
<% case 1 %>
   <!--#include file="navie.inc"-->    
<% case 2 %>    
  <!--#include file="navnet.inc"-->          
<% case else %>    
  <!--#include file="navnet.inc"-->      
<% End select %>

Tom
0
 
LVL 18

Author Comment

by:bruno
ID: 2195635
Tom,

wow, this is POOR...on my part and IE's part.  I figured out the problem.  When i first started trying to do this whole ASP thing, i was using IE 4.  Hence, the first code that i tried (in my original question) worked fine.  I then upgraded to IE 5....i just retried the original code for JUST the browser detection.  didn't work.  So MY bad, i apologize for not realizing i had changed browsers.  For some reason the code isn't working with IE 5.  I tried the code you posted:

<%
dim BrowserType
set Br = server.createobject("MSWC.BrowserType")
response.write Br.browser
if Br.browser = "IE" then
BrowserType = "MSIE"
end if
%>

Response.Write came up "Unknown".  It isn't recognizing IE 5!!  and i should have been smart enough to realize that i had started my testing with IE 4....very sorry!  So...know what?  is there any way to incorporate IE 5 into the ASP?  why wouldn't it recognize it?

BRUNO

PS - Tom I appreciate you sticking through this after everyone else seems to have given up.
0
 
LVL 9

Expert Comment

by:TTom
ID: 2197547
Bruno:

Very interesting.  All the testing I have been doing (successfully) was done using IE5!  I wonder if there is a "browscap" problem?  I think MSWC.BrowserType uses browscap.ini.  Perhaps the version you have is pre-IE5.  Don't really know.

Very bizarre!!

I just ran:

set br=server.createobject("MSWC.BrowserType")
response.write br.browser

on my machine at home (running IE5) and the result was
"Netscape"

Is that strange, or what?  Think I will go truck on off to www.microsoft.com and look for a new browscap.ini.  See if that makes any difference.

Tom
0
 
LVL 18

Author Comment

by:bruno
ID: 2197612
Tom,

The testing i have been doing is through PWS on my computer right here.  I uploaded these files to my server (www.halfpricehosting.com) and tested them again, using IE 5.  no problems!  The thing is, i have already updated my browscap.ini file, after jmcvinney mentioned it.  i downloaded my new one from...
http://www.cyscape.com/browscap/

That's very strange about IE5 returning Netscape!  
Well...I'm glad i figured out what the problem was...let me know if you find a browscap file that works, because I would like to be able to do my testing on my machine here, instead of uploading all my files before i have to do it.  Though i suppose i can just use IE 4 for local testing.  

I just checked my browscap.ini file, and it does have IE 5 listed...weird.

BRUNO
0
 
LVL 9

Expert Comment

by:TTom
ID: 2201039
Bruno:

One of the problems was browscap.ini.

I got the latest browscap.ini from cyscape.  Now I am getting "unknown" for IE5 (at home on PWS/Win95).  Will check browscap.ini at work, since that one worked fine, and let you know.

Tom
0
 
LVL 9

Expert Comment

by:TTom
ID: 2202955
Bruno:

Just got in to work and checked again.  Here I am running PWS on NT4 and IE 5.00.2314.1003.  Browscap.ini is 26kb, dated 4/29/99 11:04am.  It returns IE for br.browser.  I guess I will send a copy of this version home and see if that changes the results there.

Will let you know how it turns out.

Tom
0
 
LVL 9

Accepted Solution

by:
TTom earned 680 total points
ID: 2205658
Bruno:

Hope you are still with me.

I sent a copy of my browscap.ini from work to home.  I compared the entries for IE5 to see if I could find out why the latest one from cyscape was not identifying IE5 when the one from work was.  FWIW, I have NO IDEA where the one from work came from, but I suspect it's a M$ version which got installed with IE or one of the service packs (or one of the other numerous upgrades/additions/fixes from M$ that I have run on my machine).

To keep a long story from getting longer, if you open browscap.ini and look for the IE 5 section, each of the entries has an "identifier" string that looks like:

[Mozilla/4.0 (compatible; MSIE 5.0; Windows 95)*]

In my case, changing that to:

[Mozilla/4.0 (compatible; MSIE 5.0; Windows 95*)]

(which was ALMOST the syntax from the version I had) worked fine, and browscap now successfully identifies IE5 on my machine at home.

If I wanted to test this further, I would download the latest browscap to my machine at work and see whether or not it works under NT or whether the entries need to be modified.

I have forwarded my "conclusions" to the people at cyscape, so I will see what they have to say.

I mentioned that my modified syntax was ALMOST identical.  The only difference was that, in my original files, "5.0" was "5.*".

In any case, I hope this information may be useful to you, and that we can achieve a satisfactory answer to your question.

Catch you later,

Tom
0
 
LVL 18

Author Comment

by:bruno
ID: 2205675
Tom,

definitely still with you.  i will try changing the syntax and let you know how that works.  the points are definitely yours, don't worry about that!


bruno
0
 
LVL 18

Author Comment

by:bruno
ID: 2206460
Tom,

Bingo!!  

[Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)*]

got changed to :

[Mozilla/4.0 (compatible; MSIE 5.0; Windows 98*)]

Now works fine on IE5 on PWS on my machine.  Here are the points, and thank you for all your help.  I'd still be interested in what Cyscape has to say about the whole thing....considering ALL the lines are set up the same way....though it makes me wonder why IE4 would have worked fine...

bruno
0
 
LVL 18

Author Comment

by:bruno
ID: 2206461
Adjusted points to 175
0
 
LVL 9

Expert Comment

by:TTom
ID: 2206602
Bruno:

Glad to be of assistance!  I will also be interested in how cyscape responds.  I suspect it has to do with how IE identifies itself, and M$ may have changed that from v4 to v5.  Certainly wouldn't surprise me.  Didn't look at the IE4 strings in the browscap from cyscape.

I looked for an updated browscap from M$, but I didn't have much luck.  If you can track one down, it might provide a clue.

Will try to remember to let you know if/when I get a response from them.

Thanks for the points!  (Propelled me close to the top of the ASP heap! <G>)

Best,

Tom
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question