Advertisement
oquidave

progress bar with ajax and jquery

Oct 25th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //implementing a progress bar with ajax and jquery
  2.  
  3. //html
  4. <form class="dom-searc-form">
  5.     <div class="input-group input-group-lg">
  6.         <input type="text" class="form-control domain-textfield"
  7.                 placeholder="Enter a domain name without the '.ug' part e.g newvision">
  8.             <span class="input-group-btn">
  9.                 <button class="btn btn-default check-domain" type="button">Go!</button>
  10.             </span>
  11.     </div><!-- /input-group -->
  12. </form>
  13. <!-- progress bar -->
  14. <div class="progress"></div>
  15. <!-- show results here -->
  16. <div class="domain-search-res" style="display: none">
  17.    
  18. </div>
  19.  
  20. //css
  21. /* progress bar */
  22. .progress {
  23.     display: block;
  24.     text-align: center;
  25.     width: 0;
  26.     height: 3px;
  27.     background: red;
  28.     transition: width .3s;
  29. }
  30. .progress.hide {
  31.     opacity: 0;
  32.     transition: opacity 1.3s;
  33. }
  34.  
  35. //js
  36. $(document).ready(function () {
  37.      
  38. });
  39.  
  40. $('.dom-searc-form input').keypress(function (e) {
  41.   if (e.which == 13) {
  42.    search_domain_form();
  43.     return false;    //<---- Add this line
  44.   }
  45. });
  46.  
  47. $(".check-domain").click(function(e){
  48.     search_domain_form();
  49. });
  50.  
  51. function search_domain_form(){
  52.     var domain_val = $('.domain-textfield').val();
  53.     search_domain(domain_val);
  54. }//close search_domain_form
  55.  
  56. function search_domain(domain_val) {
  57.     $.ajax({
  58.         xhr: function () {
  59.             var xhr = new window.XMLHttpRequest();
  60.             xhr.upload.addEventListener("progress", function (evt) {
  61.                 if (evt.lengthComputable) {
  62.                     var percentComplete = evt.loaded / evt.total;
  63.                     console.log(percentComplete);
  64.                     $('.progress').css({
  65.                         width: percentComplete * 100 + '%'
  66.                     });
  67.                     if (percentComplete === 1) {
  68.                         $('.progress').addClass('hide');
  69.                     }
  70.                 }
  71.             }, false);
  72.             xhr.addEventListener("progress", function (evt) {
  73.                 if (evt.lengthComputable) {
  74.                     var percentComplete = evt.loaded / evt.total;
  75.                     console.log(percentComplete);
  76.                     $('.progress').css({
  77.                         width: percentComplete * 100 + '%'
  78.                     });
  79.                 }
  80.             }, false);
  81.             return xhr;
  82.         },
  83.         method: 'POST',
  84.         url: get_base_url()+"check_domain",
  85.         dataType: 'json',
  86.         data: { domain: domain_val },
  87.         success: function(data) {
  88.            // console.log(data);
  89.             $('.domain-search-res').html('');
  90.             $(".domain-search-res").css('display', 'block');
  91.             for (i = 0; i < data.length; ++i) {
  92.                 console.log(data[i]);
  93.                 domain_look_res(data[i], i);
  94.             }
  95.         },
  96.         error: function() {
  97.             console.log('There was an error');
  98.         }
  99.     });
  100.    
  101. }//close search_domain
  102.  
  103. function domain_look_res (data, id) {
  104.     var d_html = domain_html(id);
  105.     $('.domain-search-res').append(d_html);
  106.     if (parseInt(data.status)) {
  107.         $('div.dom').addClass('available');
  108.         $('#' + id + ' span.domain-el').addClass('domain-avail');
  109.         $('#' + id + ' span.domain-el').removeClass('domain-unavail');
  110.         $('.domain-name').text(data.domain_name);
  111.         $('#' + id + ' .domain-tld').text(data.domain_tld);
  112.         $('#' + id + ' .availability').text('Available at ' + data.domain_price);
  113.     }else{
  114.         $('div.dom').removeClass('available');
  115.         $('#' + id + ' span.domain-el').removeClass('domain-avail');
  116.         $('#' + id + ' span.domain-el').addClass('domain-unavail');
  117.         $('.domain-name').text(data.domain_name);
  118.         $('#' + id + ' .domain-tld').text(data.domain_tld);
  119.         $('#' + id + ' .availability').text('Taken');
  120.     }
  121. }//close domain_look_res
  122.  
  123. function domain_html(id) {
  124.     var html = '<div id=' + id + ' class="dom">'+
  125.         '<a href="#"> ' +
  126.             '<span class="domain" >' +
  127.                 '<span class="domain-name domain-el"></span>' +
  128.                 '<span class="domain-tld domain-el"></span>' +
  129.             '</span>' +
  130.             '<span class="availability domain-el"></span>' +
  131.         '</a>' +
  132.     "</div>";
  133.     return html;
  134. }//close domain_html
  135.  
  136. function get_base_url() {
  137.     var hostname = window.location.origin;
  138.     var base_dir = window.location.pathname.split('/')[1];
  139.     var base_url = hostname + '/'+ base_dir + '/';
  140.     return base_url;
  141. }//close get_base_url
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement