Automating Clientside Script Insertion Using ASP

Contributed by
Rating: 3 stars3 stars3 stars3 stars3 stars / 4
July 24, 2000
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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.


    <!--#include virtual="/script.asp"-->
    .....
    <SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
    <!--
    <%
    Dim func_arr(1),RelativeFilePath
    func_arr(0)="function foo()"
    func_arr(1)="function foo1()"
    RelativeFilePath = "includes/client_script.asp"
   
    call Print_Client_Script(func_arr,"includes/Client_Script.asp")
    %>
    //-->
    </SCRIPT>
    ......
   

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!.


///the subroutine///////script.asp///begin////////////////////////////////////

CONST MAX_ITERATIONS = 65536        '64K

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

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials