Javascript in VB class

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

Is there a way to launch a javascript command from within VB code? For
instance, to issue a window.open(some url) if a certain condition is met. I
know you can add the javacript to a control event at runtime, but i need to
just launch it, not wait for the user to click something.

Thanks
 
No. "VB Code" is server-side. JavaScript is client-side. And never the twain
shall meet.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Just bind your javascript to the appropriate client side event (such as the
the onload attribute of the body tag). It will be always performed client
side anyway.

Patrice
 
Brian Henry said:
Is there a way to launch a javascript command from within VB code? For
instance, to issue a window.open(some url) if a certain condition is met. I
know you can add the javacript to a control event at runtime, but i need to
just launch it, not wait for the user to click something.

JavaScript is just text in a response the server sends to the client. Unless
you are sending text to the client, there will be no JavaScript. The only
time you can send text to the client is at the end of the request.

Also, you seem to have some code, and you want that code to launch some
JavaScript. But how did that code get called if the user didn't click
anything?
 
It's kind of like a validation function, kind of hard to explain.
Basically, when the user clicks a button, i want to run a small function,
and then if and only if a certain condition fails do i run the javascript.
 
Brian Henry said:
It's kind of like a validation function, kind of hard to explain.
Basically, when the user clicks a button, i want to run a small function,
and then if and only if a certain condition fails do i run the javascript.

I'm sorry, but I'm afraid you're should learn a bit more about the execution
model of ASP.NET before tackling this task. Perhaps this will help: The
ASP.NET Page Object Model
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/htm
l/aspnet-pageobjectmodel.asp?frame=true). Realize that this happens on the
initial request, and on _every_ postback.
 
If you want to validate something in client side code before you
submit, bind a client side event to your submit button.


**** server code ****
myBtn.Attributes.Add("OnClick","return Validate();");


**** client code ****
function Validate() {
// do some logic
return true or false;
}

when the use clicks the button, the client side validation method is
called, and if you return true, the form will post.
 
Eg. VB.Net code:

If flag = true the
response.write("<script language = 'javascript'>alert('the flag was
succesful');</script>")
else
response.write("<script language = 'javascript'>alert('the flag was
Unsuccesful');</script>")
end if


Sagar.
 
Anand Sagar said:
Eg. VB.Net code:

If flag = true the
response.write("<script language = 'javascript'>alert('the flag was
succesful');</script>")
else
response.write("<script language = 'javascript'>alert('the flag was
Unsuccesful');</script>")
end if

This VB.NET code will have been executed in response to a user action, e.g.
a click.
 
What if I wanted to call my of function test() ? How would I do that?

Response.Write("<script language = 'javascript'>test();</script>");

....dows not work
 
Alexander Kaplunov said:
What if I wanted to call my of function test() ? How would I do that?

Response.Write("<script language = 'javascript'>test();</script>");

...dows not work

Could you please provide some detail? Exactly how does it "not work"?

And did you define a function called "test"?
 
Yes I do have test function defined.

When I execute the code I get error on the page "Error: Object expected"

I can execute alert(), window.open(), etc. functions but not functions that
I define.

Thanks for your help.
Alex.
 
Alexander Kaplunov said:
Yes I do have test function defined.

When I execute the code I get error on the page "Error: Object expected"

I can execute alert(), window.open(), etc. functions but not functions that
I define.

Can you create a reproducer and show us the code?
 
Sure. Here is it:

Client code:

function test()
{
alert("Got test!");
}

Code-behind (server)
public void Button2_Click(object sender, System.EventArgs e)
{

Response.Write("<script language='javascript'>test();</script>");

}



Thanks, Alex.
 
You cannot call a explicitly written at design time js function from a
Response.Write call. You have to write the complete text of the function at
run-time

Here is a VB.Net code for it.

Dim JString as String

jString = "function Test(){"
jString += "alert('this is a line inside the finction');"
jString += "}"

Response.Write("<script language = 'javascript'>" + jString + "</script>")
 
This code does not cause an error but it does not call the Test() function
either. Basically, nothing happens.

Alex.
 
Back
Top