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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call a javascript function from an ASP file 2

Status
Not open for further replies.

Stewart531

Programmer
Feb 18, 2003
36
US
Hi, I'm trying to call a javascript function from an ASP file. I've seen examples of this imbedded into a button using an onClick event. I would like to know if there are any alternatives to this, as I would like to call a javascript function whenever the ASP code is run, not only after an onClick event. Is this even possible? If so, how and where do I include the javascript code in the ASP file?

Thanks,
Dave
 
Stewart,

You could also call it whenever the page is loaded. For example:

<script language=&quot;javascript&quot;>
function jsfunction(){
//do something here
}
</script>

<BODY onload=&quot;jsfunction()&quot;>

This would then call that function each time the page is loaded. Hope that answers your question? Everything is absolute. Everything else is relative.
 
You can't explicitly call the javascript function from the ASP page. The ASP page is executing on the server and therefore it is not processing client-side code(like javascript), only the server-side portion. When it is finished it finishes sending the page to the client (browser) and exits, cleaning up after itself and deallocating the memory that was in use. At this point the client browser has just finished displaying the page and started registering events and parsing the scripts embedded in it (client-side scripts like javascript in script tags). At this point the client-side javascript can execute, but since the server-side script no longer exists they cannot interact. There is also the small problem that they were executing on two seperate machines that don't really every connect during the process.

Executing the code during a page event is really the only way you can execute javascript code on a web page. It is possible to fake calling javascript from the server, but that is all.

For example, you can write javascript commands straight into the script tags without putting them in a function. These commands will fire as the browser starts to process the page. If you turn the Response.Buffer off so that the server sends the html to the client as soon as it processes parts of the script rather than weaiting until it has processed each one, than you can output javascript to the browser that will run before the page has fully loaded.

Here is an example of a loading page I made at some point to show this principle:
Code:
<%
Response.Buffer = False
%>
<html>
<head>
<script language=&quot;JavaScript&quot;>
<!--
document.writeln(&quot;Script block 1 is done&quot;);
//-->
</script>
<%
'Some dummy code that is pretending to do a lot of server-side calculations
			Dim TESTLOADTIME
			TESTLOADTIME = 2
			Dim testTime, testChk

			'just a loop to slow the loading
			testTime = Now()
			testChk = second(Now()) + TESTLOADTIME
			Do Until second(testTime) >= testChk
				testTime = Now()
			Loop
%>
</head>
<body onLoad=&quot;alert('The page has finished loading now.');&quot;>

</body>
</html>

Now there isn't much javascript executing before the server has finished processing, but it should be enough to show the concept.

-Tarwn ________________________________________________
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top