0

I currently call a Perl script from my website like this to produce a Captcha image like this:

<script language="JavaScript1.2" type="text/javascript" src="http://www.MyUrl/cgi-bin/Captcha-Image.pl"></script>

And I return Captcha Image to the web page like this:

<script language="JavaScript" type="text/javascript">
                <!--
                document.write (CAPTCHA);
                //-->
              </script>

This works fine but the issue I'm having is if the user clicks back the Captcha image does not refresh and needs to do so, as after it is submitted it is no longer valid.

So I'm trying to use this:

<input type="hidden" id="refreshed" value="no"/>

<script type="text/javascript">

onload = function () {
  var e = document.getElementById("refreshed");
  if (e.value == "no") e.value = "yes";
  else { e.value = "no"; location.reload(); }
}

</script>

But instead of refreshing the page I would like to just recall the Perl script Im already using the "Cache-Control: no-cache tag" "http://www.MyUrl/cgi-bin/Captcha-Image.pl" to print a new image I tried this with no luck:

onload = function () {
  var e = document.getElementById("refreshed");
  if (e.value == "no") e.value = "yes";
  else { e.value = "no"; location.reload('http://www.MyUrl/cgi-bin/Captcha-Image.pl'); }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Blnukem
  • 173
  • 3
  • 13
  • Create the ` –  Dec 09 '17 at 15:00
  • 1
    Another way is to set `Cache-Control: no-cache` as header when you send the image. –  Dec 09 '17 at 15:04
  • Possible duplicate of [Better way to prevent browser caching of JavaScript files](https://stackoverflow.com/questions/4206224/better-way-to-prevent-browser-caching-of-javascript-files) –  Dec 09 '17 at 15:06
  • Already using the "Cache-Control: no-cache tag" – Blnukem Dec 09 '17 at 15:11
  • All I need to do is call "http://www.MyUrl/cgi-bin/Captcha-Image.pl" and not "location.reload();" Everything else works fine. – Blnukem Dec 09 '17 at 15:42
  • I see; add an id to your script: ``. To reload, try: `captcha.src = "MyUrl/cgi-bin/Captcha-Image.pl";` –  Dec 09 '17 at 16:01
  • `location.reload('http://www.MyUrl/cgi-bin/Captcha-Image.pl');` - that's not how that works ... Yes, this method accepts a _boolean_ parameter to specify whether a reload from the server is required, or re-serving the document from cache would be fine; but this works for the _main_ HTML document you are showing, not individual external resources like JavaScript embedded into it. You will either have to reload the whole document, so that the script can be loaded again _together with it_ - or find another way for your script to realize it needs to somehow generate or request fresh data. – CBroe Dec 10 '17 at 10:15

0 Answers0