Problem sharing script code on client and server side 
Author Message
 Problem sharing script code on client and server side

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



Sat, 31 Aug 2002 03:00:00 GMT  
 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



Sat, 31 Aug 2002 03:00:00 GMT  
 Problem sharing script code on client and server side
Why don't you move the <% and %>  from the file test.vbs into the file
server.asp?
Quote:

> 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



Sat, 31 Aug 2002 03:00:00 GMT  
 Problem sharing script code on client and server side
Because what's inside <%...%> must be executable script code and <!-- #include... --> is an ASP
directive, not script code.

--
Michael Harris
MVP Scripting

Why don't you move the <% and %>  from the file test.vbs into the file
server.asp?

Quote:

> 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



Sun, 01 Sep 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. ASP referring to server side variables in client side script

2. Client side and Server side script working together

3. JScript code for both server side and client side

4. Client side scripting / server side scripting

5. Accessing Data generated with client-side script from server-side script

6. Calling Server Side Script from Client Side Script

7. Call Server Side Script From Client Side Script

8. Client side scripting / server side scripting

9. ? Server side or client side problem

10. Client Side + Server Side Scripts

11. Client Side and Server Side Scripts

12. Server Side writing Client Side scripts

 

 
Powered by phpBB® Forum Software