• home
  • forum
  • my
  • kt
  • download
  • Intro to VBScript

    Author: 2007-06-24 20:55:49 From:

    Collection of 5 useful VBScript tricks.

    1. Detect the IP address of your visitors!
    Get the IP address of your visitor by running: Request.ServerVariables("REMOTE_HOST")
    E.g. your IP address is: 222.137.222.174
    Useful? Not always, but sometimes you know whom the owner of the IP address is. And then you can display a different page, or maybe a greeting, when that person visits.

    2. Detect the browser!
    This is a bit complicated. You see you can't get the browser name directly, but all browsers sends out an ID string. And from that string you can decide which browser it is. To get this ID string run Request.ServerVariables("HTTP_USER_AGENT")
    Your browser is sending out this ID string: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Alexa Toolbar)
    There is a file with all the ID strings listed, and it's called BROWSCAP.INI. Since new browsers pop out faster than ASP errors, you must remember to update your script once in a while. This ID string does not only giving away the users browser, but also the OS (e.g. win95), version and language. Take a look at this little collection of ID strings:

    ID string:Browser:Version:OS:Language:
    Mozilla/4.08 [en] (Win95; I ;Nav)Netscape Navigator4.08Win95English
    Mozilla/4.0 (compatible; MSIE 4.01; Windows 95;)Internet Explorer4.01Win95-
    Mozilla/4.0 (compatible; MSIE 5.0; Windows 98;)Internet Explorer5.0Win98-


    3. Save time with include files!
    Often you got a heading on every page of your site. Maybe it contains a logo, a menubar, or something completely different. But what if you find out that you made a typo, and that you have done the same mistake on every page (cut'n paste is fast way of doing things, including mistakes). Then you fire up a texteditor and manually change every page (all 345 of them). The solution is include files. It's an incredibly useful feature that lets you add the content of a file into other files. Take a look at this example:

    File1: The header file. head.asp
    <CENTER><IMG SRC="logo.gif"></CENTER><BR>
    <CENTER>
    <B>Menu:</B>
    <A HREF="content.asp">First page</A>
    <A HREF="links.asp">My links</A>
    <A HREF="friends.asp">My friends</A>
    </CENTER><BR>

    File2: The content file. Content.asp
    <BODY>
    <BODY BGCOLOR="#FFFFFF">
    <!--#INCLUDE FILE="head.asp"-->

    <B><CENTER>This is the first page</CENTER></B>
    <CENTER>Click the links above</CENTER>

    </BODY>
    </HTML>

    Now click here to view Content.asp (file2)
    As you see, the code in page1 is merged into page2. And there are no frames here. You can make a zillion different content pages, sharing the same header file. When the header file is updated, so is every content page. Smart or what?

    People tend to do 2 mistakes with include file, the first is the difference between:
    <!--#INCLUDE VIRTUAL="head.asp"-->
    and
    <!--#INCLUDE FILE="head.asp"-->
    When you use the key word VIRTUAL you say to ASP that the file you want to include is located in the root dir of your web server, if you use the FILE key word you tell ASP that the file to include is located in the same directory as the ASP file you have the include statement in.

    The second mistake is this:
    <%
    'THIS SCRIPT WILL NOT WORK!
    FileName = "banner.asp"
    %>
    <!--#INCLUDE FILE="<%=FileName%>"-->

    So why doesn't this work? The problem is that include files is included to each ASP file before the ASP is run.

    4. Hide your errors!
    Face it, you're not Superman, bugs do occur in your script. You just can't plan for every possible occasion. And what is more embarrassing than a big fat error message where your webpage should have been? Use the ON ERROR RESUME NEXT command to force the script to continue with the next statement when it encounters an error. It does nothing to fix the error, but hopefully it will output a decent page anyway.

    5. Make subroutines!
    If you execute the same group of statements more than one time in your script, consider using a subroutine. A subroutine is a batch of statements that is run every time you call it.
    <HTML>
    <BODY>
    <%SUB Say_hi%>
    The subroutine says hi!<BR>
    <%END SUB%>
    Welcome to my page.<BR>
    <%Say_hi%>
    <%Say_hi%>
    <%Say_hi%>
    </BODY>
    </HTML>
    Click here to see the result.


    Hope this could inspire you a bit!

    discuss this topic to forum

    relation tutorial

    No relevant information

    New

    Hot