How to create a Server Object in a Function?

T

thisis

Hi All,

i have this.asp page:

<script type="text/vbscript">
Function Body_Onload()
' create the object
Set obj = Server.CreateObject("UploadImage.cTest")
' use method of the object
Body_Onload = obj.cTestDelete
Set obj = Nothing
End Function
</script>

<html>
<body onload="Body_Onload()">
</body>
</html>

the this.asp gives an Error while running:
Line 4
Char 1
Error Object required 'Server'
Code 0
URL http://127.0.0.1/this/this.asp

my Question:
How to create a Server Object in a Function?
 
M

Mike Brind

thisis said:
Hi All,

i have this.asp page:

<script type="text/vbscript">
Function Body_Onload()
' create the object
Set obj = Server.CreateObject("UploadImage.cTest")
' use method of the object
Body_Onload = obj.cTestDelete
Set obj = Nothing
End Function
</script>

<html>
<body onload="Body_Onload()">
</body>
</html>

the this.asp gives an Error while running:
Line 4
Char 1
Error Object required 'Server'
Code 0
URL http://127.0.0.1/this/this.asp

my Question:
How to create a Server Object in a Function?

You can't call server-side code directly from client-side code. You'll need
to send an asynchronous http request from the browser to a server-side
script which can then make calls to Server.CreateObject. Some people call
it Ajax. Have a look here: http://www.w3schools.com/ajax/
 
A

Anthony Jones

Mike Brind said:
You can't call server-side code directly from client-side code. You'll need
to send an asynchronous http request from the browser to a server-side
script which can then make calls to Server.CreateObject. Some people call
it Ajax. Have a look here: http://www.w3schools.com/ajax/

Why does it need to asynchronous? I many cases this is undesirable.
 
T

thisis

Mike said:
How do you mean?

Hi Mike Brind,

I looked at the link you gave, thanks.

I don't understand what's ajax got to do with my question in the start
of this thread:

How to create a Server Object in a Function using VBScript?
 
M

Mike Brind

thisis said:
Hi Mike Brind,

I looked at the link you gave, thanks.

I don't understand what's ajax got to do with my question in the start
of this thread:

How to create a Server Object in a Function using VBScript?

Read the first line of my first response again: you can't call server-side
code directly from client-side code.

Your error occurs when you make a call in your function to
Server.CreateObject. Note: the big hint here is SERVER.CreateObject. ASP
code runs on the web server that the site is housed on, so you can only put
Server.CreateObject in ASP code. You can't put it in client side VBScript -
your Body_Onload() function, which runs on the user's browser - not the web
server.

The client side VBScript will only run after the page has finished executing
on the server and the response has been sent to the client. As far as the
web server is concerned, the page has finished and doesn't exist anymore.
Therefore, if you want to make use of server-side functionality once the
page has been assembled in the user's browser, you need to either get the
user to post the entire page back and respond to that event, or make use of
some event on the page to do a kind of partial post-back where the page
stays in the user's browser. This is where ajax comes in.

However, it seems to me pointless to make a call to server-side script when
the html body is still loading in the user's browser. Maybe someone else
will provide some obscure examples of when this is the right thing to do,
but at that point, there is no chance for user interaction, so you should
really move your Server.CreateObject to your ASP code in my view.
 
A

Anthony Jones

Mike said:
How do you mean?

In most cases further processing of subsequent code would be nonsense until
the result of the function has returned. It also leaves the UI live and
accepting other user events, handling these correctly whilst an outstanding
operation is in effect can be tricky.

I was wondering why this case would warrant it? It's difficult to see what
Thisis intends since the code appears to be doing some very strange things
to start with e.g.:-

Body_Onload = obj.cTestDelete
 

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,139
Latest member
JamaalCald
Top