//  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 :: Can I include a file in both server-side and client-side script?


Can I include a file in both server-side and client-side script?

Often, ASP developers want to put common functions in a place accessible to both client-side and server-side scripts. There are some challenges with this, including the fact that several server-side objects (such as Response, Request) are not available on the client, much like several client-side elements (such as document, window and msgbox) are not available on the server. But if you write a routine that's generic enough, it can make sense to have it serve both the client-side and server-side scripts - reducing maintenance in the long run. 
 
 
If you're using IIS 5.0 or better, you can achieve this simply as follows. Let's say you have a .js file called 'whatever.js': 
 
    function returnValue() { return "value"; }
 
Now you can call this from both server-side and client-side JScript like this: 
 
<script language=jscript runat=server src=whatever.js></script> 
 
<script language=jscript runat=server> 
Response.Write("Server: "+returnValue()+"<br>"); 
</script> 
 
<script language=jscript src=whatever.js></script> 
 
<script language=jscript> 
document.write("Client: "+returnValue()); 
</script>
 
Now, for those using IIS 4.0: 
 
I'm going to provide an example of using a VBScript routine in server-side and client-side VBScript, as well as using a JScript routine in server-side and client-side JScript. I'm not going to attempt to crosswire the languages, nor am I going to try to deal with workarounds for swapping out document / response depending on the scope. 
 
Here is an example of using a VBScript routine in client-side and server-side script. You'll need a file called fixString.asp: 
 
<% 
    function fixString(str) 
        fixString = replace(str,"Bob","the coder formerly known as Bob") 
    end function 
%>
 
And another ASP file, with this code: 
 
<!--#include file='fixString.asp'--> 
<% 
    response.write fixString("Do you think that's okay with Bob?") 
    set fso = CreateObject("scripting.FileSystemObject") 
    set fs = fso.OpenTextFile(server.MapPath("fixString.asp")) 
    f = fs.ReadAll() 
    fs.close: set fs = nothing: set fso = nothing 
 
    ' we just replace the server-side delimiters with client tags 
    f = replace(f,"<" & "%","<" & "script language=vbscript>") 
    f = replace(f,"%" & ">","</script>") 
    response.write(f) 
%> 
 
<script language=vbscript> 
    msgbox fixString("I don't think Bob will mind."),64,"Bob" 
</script>
 
Here is an example of using a JScript routine in client-side and server-side script. You'll need a file called fixStringJS.asp: 
 
<script language=JScript runat=server> 
function fixString(str) 

    return str.replace("Bob","the coder formerly known as Bob"); 

</script>
 
And another ASP file, with this code: 
 
<!--#include file='fixStringJS.asp'--> 
<Script language=JScript runat=server> 
    Response.Write(fixString("Do you think that's okay with Bob?")); 
    var fso = new ActiveXObject("Scripting.FileSystemObject"); 
    var fs = fso.OpenTextFile(Server.MapPath("fixStringJS.asp")); 
    var f = fs.ReadAll(); 
    fs.close(); var fs = null; var fso = null; 
 
    f = f.replace(" runat=server",""); 
    Response.Write(f); 
</script> 
 
<script language=Jscript> 
    alert(fixString("I don't think Bob will mind.")); 
</script>
 
Now, this fictitious example doesn't deal with a serious limitation in JScript's implementation of replace - it only replaces the *first* instance of the search string, not *all* instances (like VBScript). Not sure which language implemented it wrong; according to the ECMA docs, JScript is correct. But I prefer the behavior of VBScript. I'll leave it as an exercise to the reader to code a workaround in JScript.

Related Articles

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 get the name of the current URL / page?
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: 11/1/2001 | Last Updated: 3/24/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (314)

 

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