Call a sub function using javascript

Posted on 2007-12-03
Last Modified: 2008-02-01
I have a hyperlink with destination url="javascript:Process()". Then on my aspx page, I have a sub function in the script section:

Sub Process()
code here...
End Sub

I also have a javascript in the head section, which i would like to initiate the "Sub Process()" When the hyperlink is clicked. The javascrpt I have so far is this:

<SCRIPT language=JavaScript type=text/javascript>
            function Process(){
                <% Process() %>

When the hyperlink is clicked I need it to do the Process() sub. I cannot use a linkbutton, or else I would have already done that. Thank you.
Question by:mlg101
  • 3

Expert Comment

ID: 20398638
Is this supposed to be VB.NET code?

Sub Process()
code here...
End Sub

If so, that's server code only - javaScript wouldn't be able to run it since it runs only on the client.  What you want isn't possible... unless I'm totally missing something here :)

Expert Comment

ID: 20398644
As a side note, you *could* get javaScript to execute something on the server but it'll require some re-working as well as AJAX.
LVL 28

Expert Comment

ID: 20398652
Hmm, so on the client you have code running. And then you wan to call a function that is located on the server.

There is only one way to do that: make the client do a (http-) request to the server. Pass the parameters in the request, and send the result back to the client. The simplest way to do it is to submit a form, and refresh the whole page. But it is also possible to to only refresh the page partly. That is what AJAX does.

There are many ways to apply AJAX functionality. The all involve the xmlhttprequest object. If you do some searching on that, you will find plenty of examples. But AJAX isn't very easy. There are downloadable kits that which will help you.

Anyway, are you sure that there is no other solution? Why can't you run the function on the client?


Author Comment

ID: 20398722
What I'm trying to do is call the process sub (yes is VB), from a hyperlink control. The hyperlink does not have an onclick property, so I cant use that.

The reason is that I'm using ASP.NET menu controls, populated by a sitemap, so I have to specify the url to link to in the sitemap, (eg., <sitemapnode url="", title="", etc.). There is no way to put a linkbutton in the sitemap, to get it on the menu control. Does that make more sense as to waht I'm trying to do?

Accepted Solution

Chumad earned 500 total points
ID: 20398773
Can you reference the page again and pass in the instructions then in page_load check for them and run the process? For example:

Have the URL point back to yourpage (assume it's yourpage.aspx):


Then in page_load, do a check for process=1 on the url:

if Request.Params("process") <> Null then


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP sending two datas 2 22
Accordion won't close correctly 5 12
orderby list (from Json) 1 27
Jquery if else not working ... WHY? 5 23
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question