//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
      COM / ActiveX Components
      Forms
      General Topics
      Date/Time Routines
      Email Scripts & Info
      Files/Directories & FSO
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
classicasp.aspfaq.com

ASP FAQ Tutorials :: Classic ASP 1.0 :: Files/Directories & FSO :: How do I get the name of the current URL / page?


How do I get the name of the current URL / page?

This one is pretty easy, but there are two parts. 
 
To retrieve the name of the current file, you can use any of these: 
 
<% 
    Response.Write Request.ServerVariables("SCRIPT_NAME") & "<br>" 
    Response.Write Request.ServerVariables("PATH_INFO") & "<br>" 
    Response.Write Request.ServerVariables("URL") & "<br>" 
%>
 
To make that path local (for example, to use with FileSystemObject), just apply the server.mappath() method to the result. 
 
To get the entire URL, including the http:// or https:// prefix, you can do this: 
 
<% 
    prot = "http" 
    https = lcase(request.ServerVariables("HTTPS")) 
    if https <> "off" then prot = "https" 
    domainname = Request.ServerVariables("SERVER_NAME") 
    filename = Request.ServerVariables("SCRIPT_NAME") 
    querystring = Request.ServerVariables("QUERY_STRING") 
    response.write prot & "://" & domainname & filename & "?" & querystring 
%>
 
To get the page name ONLY, use something like this: 
 
<% 
    scr = Request.ServerVariables("SCRIPT_NAME") & "<br>" 
    if instr(scr,"/")>0 then 
        scr = right(scr, len(scr) - instrRev(scr,"/")) 
    end if 
    response.write scr 
%>
 
Or, without the IF logic: 
 
<% 
    scr = Request.ServerVariables("SCRIPT_NAME") & "<br>" 
    loc = instrRev(scr,"/") 
    scr = mid(scr, loc+1, len(scr) - loc) 
    response.write scr 
%>
 
Now. If your file is an #INCLUDE within another file, the above scripts will produce the name of the CALLING file (since the included file is first integrated into the calling script, then the ASP within it is all executed in the context of the 'parent' file). One way you can work around this is to re-populate a current_filename variable before loading each include file, for example: 
 
<% 
    current_filename = "filetoinclude.asp" 
%> 
<!--#include file='filetoinclude.asp'--> 
 
(And no, don't try passing current_filename as a variable to the #INCLUDE directive; see Article #2042.) 
 
Then, in filetoinclude.asp: 
 
<% 
    Response.Write "Current file: " & current_filename 
%>
 
Of course, you could just as easily hard-code the filename inside of each include file. But I suppose that solution would somewhat defeat the purpose of retrieving that information at least somewhat dynamically.

Related Articles

Can I include a file in both server-side and client-side script?
Can I read / write a user's file without a prompt?
Can I rename a file using FileSystemObject?
Could I get some help working with files using FileSystemObject?
How do I avoid 'the red x' when an image is missing?
How do I change the modified time of a file?
How do I create / manipulate images from ASP?
How do I dynamically include files?
How do I find the owner, author, and other properties of a file?
How do I get a list of a folder's subfolders?
How do I prevent people from 'leeching' my CSS or JS files?
How do I prevent people from 'leeching' my images?
How do I retrieve a random file from a given folder?
How do I send the correct filename with BinaryWrite?
How do I sort a list of files?
How do I use FileSystemObject to create a file on the client?
Why do I get 'Disk not ready' errors with FileSystemObject?
Why do I get 'Invalid procedure call or argument'?
Why do I get 'Path not found' errors with Scripting.FileSystemObject?
Why do I get 'Permission Denied' errors with FileSystemObject?
Why do I get 800A0034 errors?
Why do I get 800A003E / Input past end of file errors?
Why do I get 800A0BBA errors?
Why do I get 800A0BBC errors?
Why do I get an 'Invalid Path Character' error?
Why do I get permissions errors after upgrading to Windows XP?
Why does FileSystemObject hang all of a sudden?
Why is 'the operation completed successfully' an error message?

 

 


Created: 8/13/2000 | Last Updated: 8/15/2002 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (304)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...