is it possible to capture the output of a function in asp?

J

Jay Donnell

I have a function that is a few thousand lines of code (I didn't write
it) and I want to capture the output and save it as a variable. This
is very easy to do in php. It would look something like this.


<?php
function myFunc(){
echo "something else"
}

//sent immediately to browser
echo "something"

//start capturing the output
ob_start();

myFunc()

$out = ob_get_clean();
//$out now contains 'something else'
//and 'something else' is never sent to the browser.

?>

Is this possible in ASP? I've searched the net and haven't been able
to find anything.
 
H

Hal Rosser

Jay Donnell said:
I have a function that is a few thousand lines of code (I didn't write
it) and I want to capture the output and save it as a variable. This
is very easy to do in php. It would look something like this.


<?php
function myFunc(){
echo "something else"
}

//sent immediately to browser
echo "something"

//start capturing the output
ob_start();

myFunc()

$out = ob_get_clean();
//$out now contains 'something else'
//and 'something else' is never sent to the browser.

?>

Is this possible in ASP? I've searched the net and haven't been able
to find anything.

your php function "myFunc()" would be considered a sub in vb because it does
not return a value.

here's a sample vb function:

Private Function SquareTheArg( x as Double) as Double
Return ( x * x)
End Function

***then to use the function***
dblAnswer = SquareTheArg( someVariable)
HTH
 
H

Hal Rosser

here's a sample vb function:
Private Function SquareTheArg( x as Double) as Double
Return ( x * x)
End Function

***then to use the function***
dblAnswer = SquareTheArg( someVariable)

OOPS - that was dot-net junk
---retry---

function SquareTheArg( x)
SquareTheArg = x * x
End Function
*****
then to use the function -
resultVar = SquareTheArg( someVariable)
****Or to show the result directly**
<%= SquareTheVar( someVariable) %>
 
M

Mark Schupp

1. include the file in a separate ASP page that calls the function. get the
new page using xmlhttp (http://www.aspfaq.com/show.asp?id=2173).

2. rewrite the function to return a string containing the output instead of
writing it out directly. The difficulty of this will depend on how the
function is currently written but this would be the "better" approach.
 
J

Jay Donnell

Ok, I don't think I explained this very well. In php I can have a
function that uses echo(response.write) to send data to the browser.
However, I can tell php to buffer it and not send it to the browser.
Then I can grab the data that is in the buffer and put it in a
variable. In my example below the line in myFunc, echo "soemthing
else"; never gets sent to the browser and I put the contents of the
buffer in the variable $out. Basically, I have some asp code with a
very big function. I want to take all the data sent with
Response.Write and the plain html and capture it before it is sent to
the browser. I know that I can do something like

sub mySub
content = content & "some html here
....
end sub

I don't want to do this because the sub is thousands of lines of code
and I don't want to change it all. In asp you can do something like
Response.buffer = true and it will put all the output in a buffer and
send it all at once rather than piecemeal. I want to be able to get at
the data in the buffer before it is sent and stop it from beind sent.
 
M

Mark Schupp

With response.buffer=true you can buffer the data and you can discard it if
you don't want to send it. However, you cannot access data in the buffer any
way that I know of.
 
D

Dave Anderson

Jay said:
...I want to take all the data sent with Response.Write
and the plain html and capture it before it is sent to
the browser...

...I want to be able to get at the data in the buffer
before it is sent and stop it from beind sent...

Which one? Do you want to stop it or capture it?

I'm not sure you can accomplish what you want without modifying the
function. By all is not lost -- you can probably construct a suitable
search/replace for Response.Write() in your function.

For example, if the ASP scripting language is JScript, you could add this to
the beginning of your function...

var out = { value:"", add:function(x){this.value+=x} }

....then search for "Response.Write(" and replace with "output.add(". At the
end, Response.Write(out.value), conditionally of course.

It's trickier if VBScript, since the parentheses are optional with
Response.Write(), and it may be inconsistently coded.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
J

Jay Donnell

Mark Schupp said:
1. include the file in a separate ASP page that calls the function. get the
new page using xmlhttp (http://www.aspfaq.com/show.asp?id=2173).

Thanks for the link. This seems like it might work well.
2. rewrite the function to return a string containing the output instead of
writing it out directly. The difficulty of this will depend on how the
function is currently written but this would be the "better" approach.

The reason I don't want to do this is that the function is literally
3000+ lines of code with html mixed in with the asp. Hopefully the
xmlhttp will work.
 
J

Jay Donnell

xmlhttp worked great. It isn't as flexible as the php buffer functions
like ob_start but it worked well for what I needed. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top