Author: Lati Matata email : mlati@yahoo.com Date : 21/07/2000 Tired of having to repeat the same Client-side support script's(JavaScript) for different pages?. This ASP Subroutine takes in the name of the function(s) as an array e.g
Dim func_arr(1) func_arr(0)="function foo()" func_arr(1)="function foo1()"
and the name of the central file that contains all the support javascript functions that you reuse (relative to the current file) e.g
dim RelativeFilePath RelativeFilePath = "includes/client_script.asp"
You can then call the function within the script tags of the page your wish to insert the javascript function.
IMPORTANT!!. 1. The JavaScript function MUST be well formed!--would work "as is"...if manually inserted. 2. The client script file size should be not more than 64k for optimal performance
I'll be happy to take any questions/comments, dont expect an immediate reply though!.
public sub Print_Client_Script(func_Name_arr,File_Name)
'func_Name_arr ---array of names of the functions to print out 'File_Name ---location of the file containing the function name(relative)
dim fileObj,file_path,fileStream,i,code_str,ForReading dim func_Start_pos,iControl,Func_length,Func_End_pos ForReading = 1 Set fileObj = CreateObject("Scripting.FileSystemObject") file_path = server.MapPath(File_Name) 'complete physical path to client script file if (fileObj.FileExists(file_path)) and isarray(func_Name_arr) = true then 'checking for file existence! Set fileStream = fileObj.OpenTextFile(file_path,ForReading) code_str = fileStream.ReadAll() 'reads the contents into one long string and then closes it 'thats the reason for the limit on file size fileStream.Close() for i = 0 to Ubound(func_Name_arr) 'iterate through list of functions to search for '(assumption the cilent script page has many other functions) func_Start_pos = instr(1,code_str,trim(func_name_arr(i)),1) 'look for the function in the string if func_Start_pos <> 0 or func_start_pos <> null then 'function name has been found Func_End_pos = func_Start_pos 'initialise the various controls iControl = 0 Func_Length = 0 do if mid(code_str,Func_End_Pos,1) = chr(123) then 'looking for opening braces--"{" iControl = iControl + 1 elseif mid(code_str,Func_End_Pos,1)=chr(125) then 'looking for closing brace--"}" iControl =iControl - 1 if iControl = 0 then 'the iControl has go up then back down to zero(a full function!!) exit do end if end if Func_End_Pos = Func_End_Pos + 1 'move to the next character loop while true AND Func_End_pos < MAX_ITERATIONS 'max_iterations..just incase..dont want to hung the server! Func_Length = Func_End_Pos - func_Start_pos + 1 'add one to include the closing brace Response.Write mid(code_str,Func_Start_pos,Func_length)&chr(13) 'right out the whole function string else 'ERROR the function has not been found! end if next
else 'ERROR the file does not exist end if Set fileObj = nothing end sub