Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Share code between Global.asa and ASP pages 1

Status
Not open for further replies.

Monkey36

MIS
May 23, 2005
60
GB
I've got a javascript class for reporting events (eg. user logged on, user supplied incorrect password, etc..) for a web app, which wraps a Server.CreateObject call, and some initialisation code.

So, in each ASP page that needs to log an event, there is an #include statement at the top of the code. However, I'd really like to be able to log an event in the Session_OnStart, so I'd like to call this class from global.asa. However, the file that's included (ie. the class file) starts and ends with the <% %> tags, which are not valid in the global.asa file.

Is there any way that I can adjust this set up so that one file can be shared by the ASP code AND the global.asa?

Maybe I'm missing something obvious, but I just can't think of a way to make one file "includable" by both.

Thanks in advance for any help that can be offered!

Cheers,
Dan.
 
the file that's included (ie. the class file) starts and ends with the <% %> tags, which are not valid in the global.asa file

If that is the only problem them perhaps you've answered your own question.
 
But if I take out the <% %> tags then I can no longer include it in my standard ASP files. And if I leave them in, then I can't use it with the global.asa.

I want to avoid having two class files for accessing the reporting object - one for standard ASP files and one for the global.asa file.
 
Alright, well this is total speculation... I wonder if Server.Execute could be called inside global.asa such that you could use an external "wrapper" page to do the work and then return to global.asa.

another "wrapper" idea would be to make some more include files... one that only contains the script open characters, another that only contain the script close characters, and a third that calls the other three. ex:
<!--#include file="open.asp"-->
<!--#include file="clsEvent.asp"-->
<!--#include file="close.asp"-->


of course this sorta flys in the face of wanting to reduce the number of files... but at least the meat of your code remains in a single file.
 
Thanks for the suggestions! I'm doing a bit of research into Server.Execute and seeing if I can come up with anything. It took me looking at your second suggestion a couple of times to see what you meant, but that may have a lot of potential.

I think I've got an idea brewing which I'm going to go try. If it works I'll post back in case anyone else ever has the same problem (otherwise I'll be back for more help :)

Cheers!
 
D'oh. Your suggestion 2 is a no-go, it causes ASP error 0116: "the script block lacks the close of script tag" - ie. each include file must presumably be a complete script block.

Back to researching Server.Execute I guess..
 
> Is there any way that I can adjust this set up so that one file can be shared by the ASP code AND the global.asa?

I remember similar problems three years ago, tried everything - SSI, <SCRIPT> tags, .Execute, blah - and finally wrapped shared code into class/.DLL and registered it with regsvr32. Maybe things changed in the meantime, dunno.

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Ok! I think I've got a working solution. It's not quite as elegant as I would have liked, but it doesn't seem to be too bad.

I've got a file "ReadJS.inc"
Code:
<%
 function ReadJS(strFile) {
  var objFSO = Server.CreateObject("Scripting.FileSystemObject");
  var strFileName = Server.MapPath(".") + "//" + strFile;
  var strContent = objFSO.OpenTextFile(strFileName).ReadAll()
  return(strContent);
 }
%>
Then I've got a simple javascript file; test.js
Code:
function TestFunc() {
 return('works!');
}
If I want to include this function in global.asa (or as client-side javascript), I can access it with
Code:
<SCRIPT LANGUAGE="JSCRIPT" RUNAT="Server" SRC="test.js"></SCRIPT>
If I want to access it from ASP (server-side) code, then I need an include for
Code:
<!-- #include virtual="/includes/readjs.inc" -->
and then to read in the javascript:
Code:
<% eval(ReadJS("test.js")); %>
And this seems to work! Now it's not perfect, but I think it's workable for my needs.

If anyone's got any comments or can see anything wrong or potentially wrong with this solution, I'd be interested to hear.

Cheers,
Dan.
 
Update: Actually ReadJS.inc should read
Code:
<%
 function ReadJS(strFile) {
  var objFSO = Server.CreateObject("Scripting.FileSystemObject");
  var strFileName = Server.MapPath("\\") + strFile;
  var strContent = objFSO.OpenTextFile(strFileName).ReadAll()
  return(strContent);
 }
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top