Template source: custom_comment_form_js.html

<script type="text/javascript"> var csrftoken = getCookie('csrftoken'); var monthNames = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]; var the_date = new Date(); $.ajaxSetup({ beforeSend: function(xhr, settings) { if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) { // Send the token to same-origin, relative URLs only. // Send the token only if the method warrants CSRF protection // Using the CSRFToken value acquired earlier xhr.setRequestHeader("X-CSRFToken", csrftoken); } } }); // Article comment form $('body[id="articles"]').on('click', '.comment_form_btn_id', function(e) { e.preventDefault(); var article_id = $(this).attr("data-id"); var $form = $('#comment_form_' + article_id); var $success_message = $form.find('.comment_form_success'); $success_message.addClass('hidden'); remove_fields_errors($form); $.post("{% url 'ajax_comment_form' %}", // Serializes the data to form a post to the request $form.serialize(), // Function to handle result function (res) { $('#message_agent_comment').html('<span>' + res['msg'] + '</span>').show(); if (res['result'] == 'success') { $('.custom_comments_' + res['article']).prepend("<div id='comment_id_new' class='custom_comments_item'><h4 class='pull-left'>" + res['username']+ "</h4><div class='pull-right comment-reply'></div><div class='clearfix'></div><small class='comment-date'><i>" + monthNames[the_date.getMonth()] + " " + the_date.getDate() + ", " + the_date.getFullYear() + ", " + the_date.toLocaleTimeString() + "</i></small><p>" + res['message'] + "</p></div>"); /** RESET FORM **/ $form.trigger("reset"); $success_message.removeClass('hidden'); /** RESET CAPTCHA **/ reload_captcha($form); /** Add 1 to load-more so it loads the nest sequence properly **/ $('.load-comments_' + article_id).find('.load-more').attr('data-load', function(i, val){ return parseInt(val) + 1; }); } else { display_fields_errors(res['message'], $form, article_id); } } ); }); // Comment reply form $('#articles').on('click', '.comment_reply_btn_id', function(e) { e.preventDefault(); var parent_id = $(this).attr("data-id") var $form_reply = $('#comment_reply_' + parent_id); var $success_message = $form_reply.find('.comment_form_success'); $success_message.addClass('hidden'); remove_fields_errors($form_reply); $.post("{% url 'ajax_comment_form' %}", // Serializes the data to form a post to the request $form_reply.serialize(), // Function to handle result function (res) { $('#message_agent_comment').html('<span>' + res['msg'] + '</span>').show(); if (res['result'] == 'success') { $('.children_of_' + parent_id).append("<div id='comment_id_new' class='custom_comments_item'><h4 class='pull-left'>" + res['username']+ "</h4><div class='pull-right comment-reply'></div><div class='clearfix'></div><small class='comment-date'><i>" + monthNames[the_date.getMonth()] + " " + the_date.getDate() + ", " + the_date.getFullYear() + ", " + the_date.toLocaleTimeString() + "</i></small><p>" + res['message'] + "</p></div>").fadeIn(); $form_reply.fadeOut(2000); /** RESET FORM **/ $form_reply.trigger("reset"); $success_message.removeClass('hidden'); /** RESET CAPTCHA **/ setTimeout(function(){ reload_captcha($form_reply); }, 2000); } else { display_fields_errors(res['message'], $form_reply, parent_id); } } ); }); $('div[id="content"]').on('click', '.comment-reply-btn', function(e){ e.preventDefault(); $('#comment_reply_'+ $(this).attr("data-id")).toggle(); }); $('#content').on('click', '.comment-view-replies', function(e){ e.preventDefault(); $('.children_of_'+ $(this).attr("data-id")).toggle(); }); $('#articles-wrapper').on('click', '.load-more', function(e){ e.preventDefault(); var id = $(this).attr('data-id'); var load_more = $(this).attr('data-load'); $.post("{% url 'ajax_load_more' %}", {article_id: id, load_more: load_more}, function(res){ $('.load-comments_' + id).remove(); $('.custom_comments_' + id).append(res); }); }); $('#commenting').on('click', '.js-captcha-refresh', function(e){ e.preventDefault(); var form = $(this).parents('form'); reload_captcha(form); }); function reload_captcha(form){ var img = form.find('.captcha'); var hidden = img.next(); $.getJSON("{% url 'reload_captcha' %}", {}, function(json) { img.attr('src', json['new_cptch_image']); hidden.val(json['new_cptch_key']); }); } function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } function sameOrigin(url) { // test that a given url is a same-origin URL // url could be relative or scheme relative or absolute var host = document.location.host; // host + port var protocol = document.location.protocol; var sr_origin = '//' + host; var origin = protocol + sr_origin; // Allow absolute or scheme relative URLs to same origin return (url == origin || url.slice(0, origin.length + 1) == origin + '/') || (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') || // or any other URL that isn't scheme relative or absolute i.e relative. !(/^(\/\/|http:|https:).*/.test(url)); } function display_fields_errors(messages, container, the_id) { Object.keys(messages).forEach(function (k) { container.find('#error_' + messages[k][0] + '_' + the_id).append(messages[k][1]); }); } function remove_fields_errors(container) { container.find('.ajax_error').each(function (k, v) { $(this).html(''); }); } function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } </script>