When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : The Nature of Things


Fundamentals of client-server ASP:   How can my ASP code call a JavaScript (or VBScript) function in the browser? How can I use a function or event in the browser to call a VBScript function in ASP?

[Print this FAQ]

Answer: Short answers: You can't, you can't, you can't.

Think about this for a moment: You are running your ASP code on a Server machine in Lower Franconia, South Texas. Your user is in an airplane flying over Zurich, Switzerland.

Question: What connection is there between your ASP code and the scripts in the browser?

Answer: NONE!

You use ASP to create an HTML page that gets sent to the browser. And once that page is created, there is no longer any connection between the browser and the web server! The web server has completely forgotten that the browser ever made an HTTP request and that it answered that request by sending back some HTML.

More than that: The browser has completely forgotten the details of the request! Once it receives the HTML, it is satisfied that the HTTP request is finished.

Nothing in the HTTP request-response protocol expects to or is expected to maintain a connection once the request is answered!

If you do not understand these concepts, then you really need to get a good book on ASP and HTML and carefully read all the sections on "how ASP works" (or whatever those parts of the book are called). And if your book doesn't explain this really well and in careful detail, find another book.

In the meantime, you might want to look at these on-line resources (and feel free to suggest additions to this list! you can email me at Bill@ClearviewDesign.com):

Other FAQs in the ASPFAQs

How do I send [information] from client-side [scripts] to server-side ASP?
How can I pop up a message from ASP?
Can I send my output to a different window or frame?

Articles on 4GuysFromRolla

ASP Basics: What's Happening Back There?

Other articles, etc.

...please contribute your favorites...

--- ADDENDUM ---

Ellery Familia justifiably noted that I do not mention Remote Scripting in any of the above. And since others might have the same comments and concerns, I thought I should discuss this avenue of coding.

If you are not familiar with Remote Scripting, I urge you to visit http://msdn.microsoft.com/scripting and click on Remote Scripting and read all about it.

In essence, Remote Scripting allows you to use a client-side component (this one happens to be written in Java, presumably so that it can be used with browsers other than MSIE) to request the results of a call to a specific function on a specific page from the server. Unlike XMLHTTP which can request any page from any site, thanks in part to the security limitations of Java, this component can only retrieve pages from the site that served up the original page. There are further restrictions, but read about them on the MSDN site.

I would like to emphasize that this is not an ability to simply and directly call a server side function. You must put some arcane code into your client-side script to make the call. It's not overly difficult code, but neither is it a direct and simple function call mechanism.

Also, you can't use Remote Scripting to call a server-side component that returns any kind of object (the most common request being for an ADODB.RecordSet).

None of that means that Remote Scripting is not useful! Just that it is not as simple to use as the questions given as the topic of this FAQ would seem to require.

Interestingly, you can accomplish effectively this same thing without using Remote Scripting. Simply use a <FRAMESET> and make one of the frames invisible (by allocating it 0% space in the window). Then you can invoke an ASP page into that invisible frame from another frame via
parent.invisibleFrame.location.href = 'xyz.asp?fld1=779&fld2=bananas';
or similar. The page that is returned from that query, into the invisible frame, could contain anything that is legal in an HTML page, couldn't it? So it could contain JavaScript code that would alter the contents or actions of the visible frame, for example. In other words, this usage is actually more powerful than Remote Scripting, if used carefully!

Still, this scheme, too, does not answer the simple request of the questions presented by this FAQ: You still can't simply and directly call a server-side function from client-side code. Or vice versa.

FAQ posted by Bill Wilkinson at 9/21/2001 3:24:20 PM to the The Nature of Things category. This FAQ has been viewed 61,858 times.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article