Problem sharing script code on client and server side
Here's one way (requires VBScript 5.0 or higher on the server):
1) use a Scripting.FileSystemObject on the server side to open and read the script code include file
as one big chunk of text...
set fso = createobject("scripting.filesystemobject")
set tsCode = fso.opentextfile(server.mappath("commoncode.inc"))
txtCodeChunk = tsCode.Readall
tsCode.close
set fso = nothing
set tsCode = nothing
2) remove the <%...%> delimiters
txtCode = replace(replace(txtCode,"<%",""),"%>","")
(You could also remove these in advance if this is the only way you'll be using the "commoncode"
file).
3) For the server,
ExecuteGlobal txtCodeChunk
This adds the code to the ASP page's server side script namespace.
4) For the client,
<script language="vbscript">
<%=txtCodeChunk%>
</script>
This sends the same code back to the client inside script tags.
In highly technical terms, this method is referred to as the "cure for the common code"...
--
Michael Harris
MVP Scripting
I have a function that I want to share between client and server. So I
put the function is a separate file and try to use #include directive to
include the source code on both client and server.
My client.asp script is followed:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=VBSCRIPT>
<!-- #include file="test.vbs" -->
</SCRIPT>
</HEAD>
<BODY>
<a onclick="Msgbox(myfunction(2,3))"> Click here please</a>
</BODY>
</HTML>
My server.asp script is followed:
<!-- #include file="test.vbs" -->
<% Response.write(myfunction(2,3)) %>
My shared function is in a separate file "test.vbs" as followed:
<%
function myfunction(a,b)
myfunction = a+b
end function
%>
This gives me the error when running client.asp because of "<%" and
"%>". Removing them, my server.asp complaints. What should I do ?
Thanks for your help in advance.
Andy Le