Skip to content

Commit 452f1e0

Browse files
authored
IE & jQuery slim compatible Staticman JS (daattali#782)
1 parent 85444cb commit 452f1e0

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

assets/js/staticman.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,42 @@ layout: null
1414
var endpoint = '{{ sm.endpoint }}';
1515
var repository = '{{ sm.repository }}';
1616
var branch = '{{ sm.branch }}';
17+
let url = endpoint + repository + '/' + branch + '/comments';
18+
let data = $(this).serialize();
1719

18-
$.ajax({
19-
type: $(this).attr('method'),
20-
url: endpoint + repository + '/' + branch + '/comments',
21-
data: $(this).serialize(),
22-
contentType: 'application/x-www-form-urlencoded',
23-
success: function (data) {
24-
$('#comment-form-submit').addClass('d-none');
25-
$('#comment-form-submitted').removeClass('d-none');
26-
$('.page__comments-form .js-notice').removeClass('alert-danger');
27-
$('.page__comments-form .js-notice').addClass('alert-success');
28-
showAlert('success');
29-
},
30-
error: function (err) {
31-
console.log(err);
32-
$('#comment-form-submitted').addClass('d-none');
33-
$('#comment-form-submit').removeClass('d-none');
34-
$('.page__comments-form .js-notice').removeClass('alert-success');
35-
$('.page__comments-form .js-notice').addClass('alert-danger');
36-
showAlert('failure');
37-
$(form).removeClass('disabled');
20+
var xhr = new XMLHttpRequest();
21+
xhr.open("POST", url);
22+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
23+
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
24+
xhr.onreadystatechange = function () {
25+
if(xhr.readyState === XMLHttpRequest.DONE) {
26+
var status = xhr.status;
27+
if (status >= 200 && status < 400) {
28+
formSubmitted();
29+
} else {
30+
formError();
31+
}
3832
}
39-
});
33+
};
34+
35+
function formSubmitted() {
36+
$('#comment-form-submit').addClass('d-none');
37+
$('#comment-form-submitted').removeClass('d-none');
38+
$('.page__comments-form .js-notice').removeClass('alert-danger');
39+
$('.page__comments-form .js-notice').addClass('alert-success');
40+
showAlert('success');
41+
}
42+
43+
function formError() {
44+
$('#comment-form-submitted').addClass('d-none');
45+
$('#comment-form-submit').removeClass('d-none');
46+
$('.page__comments-form .js-notice').removeClass('alert-success');
47+
$('.page__comments-form .js-notice').addClass('alert-danger');
48+
showAlert('failure');
49+
$(form).removeClass('disabled');
50+
}
51+
52+
xhr.send(data);
4053

4154
return false;
4255
});

0 commit comments

Comments
 (0)