Calling code-behind from javascript

N

nams

Hi Frnds,

Say I have vb code like this:




Public Sub ll()
MsgBox("heLOOOOOOOOOOOOOOOOOOOOOOOOOOOOO", MsgBoxStyle.OkOnly)
End Sub



The following script runs as soon as the page loads, no matter what
the value of b is. why is it so ?



<script type="text/javascript" language="javascript">
function ss()
{
var b = 1;
if(b==2)
{
<% ll() %>;
}
}
</script>



I want the code to get executed only if the if-condition is true and
also only when i call the script - i mean not as soon as the page
loads. Please help me.

Thanks,

Nams here
 
T

Tom

Hi Frnds,

Say I have vb code like this:




Public Sub ll()
MsgBox("heLOOOOOOOOOOOOOOOOOOOOOOOOOOOOO", MsgBoxStyle.OkOnly)
End Sub



The following script runs as soon as the page loads, no matter what
the value of b is. why is it so ?



<script type="text/javascript" language="javascript">
function ss()
{
var b = 1;
if(b==2)
{
<% ll() %>;
}
}
</script>



I want the code to get executed only if the if-condition is true and
also only when i call the script - i mean not as soon as the page
loads. Please help me.

Thanks,

Nams here

I think you can run your "ss()" function from your HTML <body> tag using the
onLoad event, such as...

<body onLoad="ss()">

That would access your ss() javascript function each time your page loads.

You might need to check that function a little closer though. You have the
variable b set to "1", and your if statement will only be used if it is "2"

Tom
 
L

Lasse Reichstein Nielsen

nams said:
Say I have vb code like this:

Public Sub ll()
MsgBox("heLOOOOOOOOOOOOOOOOOOOOOOOOOOOOO", MsgBoxStyle.OkOnly)
End Sub

Ok. I'm not entirely sure what it's supposed to do, but I'm guessing
some sort of dialog.
WHERE do you have this code?
The following script runs as soon as the page loads, no matter what
the value of b is. why is it so ?
<script type="text/javascript" language="javascript">
function ss()
{
var b = 1;
if(b==2)
{
<% ll() %>;
}
}
</script>

This is NOT the code that is sent to the browser!

The <% ll() %> is interpreted at the server, BEFORE sending a raw
text file to the browser where all ASP (or whatever you are using)
tags have been removed.

To understand why the browser acts as it does, look at the page
that it receives.

You need to understand the difference between template code run
on the server, and the output of that template code, which is sent
to a browser and run there. You cannot call the server-side
template code methods from the client-side scripts.
I want the code to get executed only if the if-condition is true and
also only when i call the script - i mean not as soon as the page
loads. Please help me.

Then do that. Check what the browser receives. Find out what part
of your server code generates what part of the page, and then change
the server code to generate something else.

/L
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top