Solved

How to call a AJAX javascript funtion when clicking a link on a CGI generated HTML page?

Posted on 2010-09-13
7
433 Views
Last Modified: 2013-12-25
My CGI application generates HTML code with links to some (precomputed) data on the server. The data is not used for anything unless the user clicks the link. Now I want to optimize the application using AJAX to generate the data on the fly only when the user clicks the link.

The following code would work in a normal HTML page:
<a href="#" onclick="AjaxFunction();return false;">link</a>

But it doesn't work in my case, I guess because the HTML page with the links was generated by the CGI. The browser looks for litterally "#" at the root of my website (www.example.com/#)
0
Comment
Question by:Phazz
  • 4
  • 2
7 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33660599
<<The browser looks for litterally "#" at the root of my website>>
Does the AjaxFunction() method gets called?
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 250 total points
ID: 33660605
when you have given onclick, href is not required, so either you can
1) remove href="#".
2) give href= "javascript:AjaxFunction()"; and remove onclick
0
 

Assisted Solution

by:djuzoran
djuzoran earned 250 total points
ID: 33660768
try:
<a href="" onclick="AjaxFunction();return false;">link</a>
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

Author Comment

by:Phazz
ID: 33660936
Thanks to both of you!

Guruvinder372's first suggestion doesn't work, but the second one works perfectly. Djuzoran's suggestion works too.

Actually I found out that my original code in the question also works! - but my AjaxFunction wasn't executed (misspelled function name, embarrassing, huh?)

Is any of the suggestions best in terms of cross-browser support?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33660959
both should works on all browsers.
0
 

Author Closing Comment

by:Phazz
ID: 33661616
Thanks for your help.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33661633
thanks for the points
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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 how to dynamically set the form action using jQuery.
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)

685 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