Javascript in VB class

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
 
K

Kevin Spencer

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.
 
P

Patrice

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
 
J

John Saunders

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?
 
B

Brian Henry

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.
 
J

John Saunders

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.
 
J

Justin Beckwith

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.
 
A

Anand Sagar

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.
 
J

John Saunders

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.
 
A

Alexander Kaplunov

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
 
J

John Saunders

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"?
 
A

Alexander Kaplunov

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.
 
J

John Saunders

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?
 
A

Alexander Kaplunov

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.
 
A

Anand Sagar

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>")
 
A

Alexander Kaplunov

This code does not cause an error but it does not call the Test() function
either. Basically, nothing happens.

Alex.
 
Joined
Oct 27, 2009
Messages
1
Reaction score
0
this is very easy...start with this....write an html page including your javascript...embedd an IE control in your vb form...call the navigate method with your html file as url address....that's it folks!






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.

Thanks
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top