Page #66 (Chapter 9 - Dealing with Multiple Browsers)

Chapter 9 - Dealing with Multiple Browsers

Visual Basic Developers Guide to ASP and IIS
A. Russell Jones
  Copyright 1999 SYBEX Inc.

Writing Browser-Specific Code
As I mentioned at the beginning of this chapter, there are three ways to include browser-specific code in WebClasses. I'll show you all of them, and you can decide which method is best for you. The methods are:
  Writing browser-specific HTML templates
  Writing browser-specific HTML from a WebClass
  Using replaceable markers to create browser-specific pages
In the preceding section, you created a project that determined the browser type in the WebClass_Start event. In this section, you'll create two simple HTML templates—one for Netscape and one for IE. If you don't have both browsers, you can still run the code and see how the two differ.
Writing Browser-Specific HTML Templates
Create a new HTML file. In the first file, enter this code:
<html><head><title>Internet Explorer Template</title></head>
<body>
<h2 align="center">Internet Explorer</h2>
<script language="VBScript">
...msgBox "This message box was created in VBScript",,"Message Boxes Have Titles!"
</script>
</body>
</html>
Save the file as IETemplate.htm. Create another HTML file and place this code in it:
<html><head><title>Netscape Navigator Template</title></head>
<body>
<h2 align="center">Netscape Navigator</h2>
<script language="JavaScript">
...alert("This message box was created in JavaScript");
</script>
</body>
</html>
Save the file as NetscapeTemplate.htm.
  Tip Did you remember to save the .htm files in a directory other than your WebClass project directory?
Create a new project directory and call it MultipleBrowsers. Create a new IIS project, rename it MultipleBrowsers, and save the project in the MultipleBrowsers directory. Rename the default WebClass to WhichBrowser and set the NameInURL property to WhichBrowser as well. Add both files as HTML templates. When the WebClass imports HTML files as templates, it does not retain the original filenames, so rename the IE template IETemplate and rename the Netscape template NetscapeTemplate.
Place this code into the WebClass_Start event:
Private Sub WebClass_Start()
    Dim isIE As Boolean
    isIE = InStr(1, Request.ServerVariables _
        ("HTTP_USER_AGENT"), "MSIE", vbTextCompare) > 0
    If isIE Then
        Set NextItem = IETemplate
    Else
        Set NextItem = NetscapeTemplate
    End If
End Sub
The Start event code determines which template to write based on the value of the Boolean variable isIE.
In the Respond event for each template, return the template with the Write-Template method:
Private Sub IETemplate_Respond()
    IETemplate.WriteTemplate
End Sub
Private Sub NetscapeTemplate_Respond()
    NetscapeTemplate.WriteTemplate
End Sub
Save the project. If you have a Netscape browser and an IE browser, navigate to the WebClass URL with each browser. You should see a message box in IE and an alert (which looks identical to a VB message box, but doesn't have a programmable title) in Netscape. If you don't have both browsers, use IE and add this code to the WebClass_Start event:
Private Sub WebClass_Start()
    Dim isIE As Boolean
    isIE = InStr(1, Request.ServerVariables _
        ("HTTP_USER_AGENT"), "MSIE", vbTextCompare) > 0
    If IsEmpty(Session("incr")) Then
        Session("incr") = 0
    Else
        Session("incr") = Session("incr") + 1
    End If
    isIE = (Session("incr") Mod 2 = 0)
    If isIE Then
        Set NextItem = IETemplate
    Else
        Set NextItem = NetscapeTemplate
    End If
End Sub
The changed code creates and increments the Session("incr") variable each time it runs. If the value is evenly divisible by two, the code returns the IE template with the VBScript message box; otherwise, it returns the Netscape template with the JavaScript alert. You can do this because IE supports both VBScript and JavaScript.
The advantage of putting browser-specific HTML in separate templates is that you can use an HTML editor to create and maintain the files. The disadvantage of putting the code in templates is that you now have two more files to maintain, vs. only one when you write the HTML from inside the WebClass.
Writing Browser-Specific HTML from a WebClass
You can easily accomplish the same result without the templates. Change the WebClass_Start event code as follows:
' Change this
If isIE Then
    Set NextItem = IETemplate
Else
    Set NextItem = NetscapeTemplate
End If
' To this
If isIE Then
    Call IEStart
Else
    Call NetscapeStart
End If
Create a new subroutine called IEStart. Copy the HTML out of the IETemplate HTML template and paste it into the IEStart routine. Select the pasted HTML and replace the double quotes with single quotes; then add Response.Write commands to the beginning of each line.
Private Sub IEStart()
    With Response
        .Write "<html><head><title>Internet " _
             & "Explorer Template</title></head>"
        .Write "<body>"
        .Write "<h2 align='center'>Internet " _
             & "Explorer</h2>"
        .Write "<script language='VBScript'>"
        .Write "MsgBox ""This message box was created " _
            & "in VBScript"", ," _
            & """Message Boxes Have Titles!"""
        .Write "</script>"
        .Write "</body>"
        .Write "</html>"
    End With
End Sub
Do the same thing for Netscape, but call the routine NetscapeStart.
Private Sub NetscapeStart()
    With Response
        .Write "<html><head><title>Netscape " & _
             "Navigator Template</title></head>"
        .Write "<body>"
        .Write "<h2 align='center'>Netscape Navigator</h2>"
        .Write "<script language='JavaScript'>"
            .Write "alert(""This message box was " & _
            "created in JavaScript"");"
        .Write "</script>"
        .Write "</body>"
        .Write "</html>"
    End With
End Sub
Save and run the project. You'll see exactly the same screens and message boxes/alerts that you saw with the template-based method.
Using Replaceable Markers to Create Browser-Specific Pages
The third method combines the two approaches by using WebClass tags for the parts of the template that are browser specific. You then substitute the appropriate content during the ProcessTag event. This approach is suitable for simple files but difficult to maintain for files that are more complex. Part of the HTML resides in the templates, part in the WebClass. Neither the person editing the HTML nor the programmer can see how the file will look or act until after it is rendered by the WebClass.
You've had plenty of practice with tag replacement at this point, so I'm not going to write out the code. If you want to test the third method, insert a WebClass tag in place of the script in one of the templates, then write out either the VBScript or the JavaScript code during the ProcessTag event.



Visual Basic Developer[ap]s Guide to ASP and IIS
Visual Basic Developer[ap]s Guide to ASP and IIS
ISBN: 782125573
EAN: N/A
Year: 2005
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net