We help IT Professionals succeed at work.

Call a javascript function

dpinto04
dpinto04 asked
on
299 Views
Last Modified: 2008-03-17
Is it possible to call a javascript function after an ASP script does its job:

Eg
numr = 2
if numr = 2 then
'call javasrcipt function here
else
'call javasrcipt function here
end if
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
get the idea?

Commented:
Some clarification:
In an ASP page all asp script executes on the server then sends the resulting page to the client where it is rendered in the client browser and the client script then executes.

So in rancys' example, only the first client script block will be written to the page that is sent to the client. Since the code in the client script block is not contained in a function, it will execute as the browser renders it. Depending on what your client script is doing, you could get an error. For example, if the script is accessing a page element that is rendered to the page after the script:

<script>
alert(window.myEl.innerHTML)
</script>
<Span id="myEl">Hello world!</Span>

This will result in an error. Since the alert script is not in a function it executes as rendered. So it will execute before the whole page is rendered and since the myEl element is after the script, it doesn't exist yet when the script executes. This however would work:

<Span id="myEl">Hello world!</Span>
<script>
alert(window.myEl.innerHTML)
</script>

If you want to run a client function after the page is fully rendered in the client browser you could try:
<Script>
function functionA()
{
     ...Your script
}
function functionB()
{
     ...Your script
}
</Script>
<%
numr = 2
if numr = 2 then
%>
<script language = "javascript">
     document.body.onload=functionA()
</script>
<%
else
%>
<script language = "javascript">
     document.body.onload=functionB()
</script>
<%
end if
%>
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.