|
|
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)Search | 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':
Now you can call this from both server-side and client-side JScript like this:
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:
And another ASP file, with this code:
Here is an example of using a JScript routine in client-side and server-side script. You'll need a file called fixStringJS.asp:
And another ASP file, with this code:
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? |