The onClick event on the referesh button for the Captcha won’t work when I click it, but this button works well when I open the Inspect mode in Chrome and click it, refreshing the captcha as expected. Here is my HTML code :
JavaScript
x
16
16
1
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
2
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
3
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
4
<div class="md-form">
5
<input type="text" id="captchaText" class="form-control" name="captcha" data-validation="required" data-validation-error-msg="<s:text name='error.required'/>">
6
<label for="captchaText">
7
<s:text name="index.captcha" />
8
</label>
9
<div class="row">
10
<button type="button" class="btn btn-info btn-deep-purple btn-md offset-md-1 col-md-5" onclick="document.getElementById('imageCaptcha').src='https://i.stack.imgur.com/SO2KY.png'">Rafraîchir  
11
<i class="fa fa-refresh" ></i>
12
</button>
13
<div style="padding:6px; padding-right:0px;" class="col-md-5">
14
<img id="imageCaptcha" src="https://i.stack.imgur.com/SO2KY.png"></div>
15
</div>
16
</div>
Here is a screenshot of my UI :
What could be the problem in my case ?
Advertisement
Answer
The issue was due to Chrome and Firefox loading the captcha from Cache in the same tab, even if response header Cache-Control was set to no-cache. The solution was to add a random string parameter to the request, this forces Chrome to reload the captcha as it will see each time a new URL for the src attribute of the image. Something like this :
JavaScript
1
6
1
function captchaReload() {
2
let randomString = (Math.random() + 1).toString(36).substring(7);
3
let imageSource = 'Captcha.jpg'+ '?random=' + randomString;
4
document.getElementById('imageCaptcha').src=imageSource;
5
}
6
See this SO question for more details on this browser behaviour.