4

How can I make a cgi script be executed when a javascript function is called? Currently I am using a small iframe and loading the cgi script there, but it does not seem to be working. here is a snippet of code:

function back()
{
    document.getElementById("hidden").src="scripts/backImage.cgi";//execute background script
    document.getElementById("scan").src="scripts/getImage.cgi";//reload the display iframe
}
Jakob Weisblat
  • 7,450
  • 9
  • 37
  • 65
  • Can you use jquery or mootools? – malonso Jan 22 '12 at 16:56
  • I don't know anything about either of those. can you suggest somewhere to learn? – Jakob Weisblat Jan 22 '12 at 16:57
  • You won't need JQuery or mootools (or any other library for that matter) to achieve this. – Jeffrey Sweeney Jan 22 '12 at 17:02
  • @JeffreySweeney even when doing it from scratch, it would be good to abstract out the browser differences behind an API. And before writing the API, it would be good to know about the existing ones to demonstrate why the new API is better. I'm guessing that you are thinking along the lines of reducing the total size of the page, which in some cases is worthwhile. But again, someone else has probably written a decent minimal Ajax API too, so it would be worth looking into that before re-implementing it. – Douglas Jan 22 '12 at 17:13
  • @Douglas Fair enough :) I was leaning on reducing total file size when I made my comment, but xmlhttprequest is pretty universal across different browsers anyway, the only real exception being that IE sees it as an ActiveX object. Because it is such a straight-forward procedure, making a user download an entire library (or even part of one) seems unnecessary for this instance. – Jeffrey Sweeney Jan 22 '12 at 17:31

3 Answers3

5

You can use XMLHttpRequest to trigger access to your cgi script.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
4

You can use jQuery.ajax, or the shorthand:

jQuery.get("scripts/backImage.cgi");
Douglas
  • 36,802
  • 9
  • 76
  • 89
0

If you can use a JS library like jquery take a look at https://stackoverflow.com/a/861808/211097.

Code snippet from that answer:

$.ajax({
    type:'get',
    url:'http://mysite.com/mywebservice',
    success:function(data) {
        alert(data);
    }
});
Community
  • 1
  • 1
malonso
  • 2,247
  • 1
  • 21
  • 33