Скрипт офлайн корзины


/ Published in: jQuery
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var buyed_once = false;
  2. $(function(){
  3. //записываем данные по корзине слева
  4. num = getCardNum();
  5. hr = window.location.href ;
  6.  
  7. root = hr.substr(0,hr.indexOf('test.zhempodvorye.ru'))+"test.zhempodvorye.ru";
  8. $(".middle-content").append("\
  9. <div class=right onclick='window.location.href=\""+root+"/test.zhempodvorye.ru/default.htm\"'>\
  10. <div class=basket style='cursor:pointer'>\
  11. <div class=block-title>Сундучок</div>\
  12. <p>Товаров: <strong>"+num+" шт</strong></p>\
  13. <p><strong><a href='#' onclick='clearCart();location.href=location.href' style='font-size:14px;color:white'>Очистить корзину</a></strong></p>\
  14. </div>\
  15. </div>");
  16. /*
  17.  
  18.   //записываем записи из общей таблицы
  19.   $('#example tbody tr td a').each(function(i){
  20.   href = $(this).attr('href');
  21.   id = href.substr(href.lastIndexOf('/')+1);
  22.  
  23.   if(getCookie(id)=='null'){
  24.   setCookie(id,$(this).parent().parent().html(),null,'../../');
  25.  
  26.   }
  27.   }); */
  28. //защита
  29. $('form.ajax-cart-form').attr('action',location.href+'#');
  30. //скрываем кнопку
  31. href = location.href;
  32. id = href.substr(href.lastIndexOf('/')+1);
  33. if(exists(id))
  34. clear_buy_button();
  35.  
  36. //при покупке
  37. $('form.ajax-cart-form').submit(function(){
  38. var cnt = $('#edit-qty').val();
  39.  
  40. var body1 = $("body").html();
  41. var match = body1.match(/<A class=model href=\"(.*?)\"/gi);
  42. if (!match){
  43. var match = body1.match(/\"model\">(.*?)<\/a/);
  44. }
  45. if (match){
  46. var match1 = match[0].match(/href=\"(.*?)\"/);
  47. id2 = match1[1].substr(match1[1].lastIndexOf('/')+1);
  48. }else{
  49. id2 = 0;
  50. }
  51. var ssize = 0;
  52. //Если есть обязательное поле
  53. if($('.required').size()>0&&buyed_once==false){
  54. if($('.required').val()==''){
  55. alert('Укажите размер');
  56. return false;
  57. }
  58. ssize = $('.required option[selected]').text();
  59. buyed_once = true;
  60. //alert(ssize);
  61. }
  62. h = location.href;
  63. id = h.substring(h.lastIndexOf('/')+1);
  64.  
  65.  
  66. card = getCookie('card');
  67. var use_1 = true;
  68. var use_2 = true;
  69. if(exists(id)){
  70. use_1 = false;
  71. if (id2>0){
  72. if (exists(id2)){
  73. return false;
  74. }
  75. }else{
  76. return false;
  77. }
  78. }else{
  79. if (id2>0){
  80. if (exists(id2)){
  81. use_2 = false;
  82. }
  83. }else{
  84. use_2 = false;
  85. }
  86. }
  87. if (use_1){
  88. if(card!=null&&card!=""){
  89. card=card+'-'+id;
  90. }else{
  91. card = id;
  92. }
  93. }
  94. if (use_2){
  95. if(card!=null&&card!=""){
  96. card=card+'-'+id2;
  97. }else{
  98. card = id2;
  99. }
  100. }
  101. clear_buy_button();
  102. setCookie('card',card,null,'/');
  103. if (use_1){
  104. var p1 = $('.catalogue-img img:eq(0)').attr('src');
  105. p1 = '../'+str_replace(p1,'../','');
  106. h = str_replace(p1,'../default.htm','');
  107. color = $('#color_zp option:selected').text();
  108. $('#color_zp_span').remove();
  109. html = $('#attr-goods').html();
  110. html+='<span id="color_zp_span">'+color+'</span>';
  111. if(ssize!=0){
  112. html+='<br><strong>Размер:</strong> '+ssize;
  113. }
  114. html+='<br><strong>Количество:</strong> '+cnt;
  115.  
  116. setCookie(id,$.trim("<td><img src='"+h+"'></td><td>"+html),null,'/');
  117. }
  118. if (use_2){
  119. var p2 = $('.catalogue-img img:eq(1)').attr('src');
  120. p2 = '../'+str_replace(p2,'../','');
  121. h2 = str_replace(p2,'../default.htm','');
  122. color = $('#color_zp2 option:selected').text();
  123. $('#color_zp_span2').remove();
  124. html = $('#attr-goods2').html();
  125. html+='<span id="color_zp_span2">'+color+'</span>';
  126. if(ssize!=0){
  127. html+='<br><strong>Размер:</strong> '+ssize;
  128. }
  129. html+='<br><strong>Количество:</strong> '+cnt;
  130.  
  131. setCookie(id2,$.trim("<td><img src='"+h2+"'></td><td>"+html),null,'/');
  132. }
  133. return false;
  134. });
  135.  
  136.  
  137. });
  138.  
  139. function clearCart(){
  140. card = getCookie('card');
  141. if(card!=null&&card!=''){
  142. if(card.indexOf('-')==-1)
  143. card = [card];
  144. else
  145. card = card.split('-');
  146. for(i=0;i<card.length;i++){
  147. setCookie(card[i],'',null,'/');
  148. }
  149. setCookie('card','',null,'/');
  150. }
  151. }
  152.  
  153. function getCardNum(){
  154. c = getCookie('card');
  155. if(c!=''&&c!=null)
  156. a = c.split('-').length;
  157. else
  158. a = 0;
  159. return a;
  160.  
  161. }
  162.  
  163. function clear_buy_button(){
  164. $('form.ajax-cart-form input[type=submit]').parent().html('<br><span style="font-weight:bold;font-size:18px" class="ok">Куплено</span>');
  165. }
  166. function setCookie (name, value, expires, path, domain, secure) {
  167. expires = 'Mon, 01-Jan-2020 00:00:00 GMT';
  168. document.cookie = name + "=" + escape(value) +
  169. ((expires) ? "; expires=" + expires : "") +
  170. ((path) ? "; path=" + path : "") +
  171. ((domain) ? "; domain=" + domain : "") +
  172. ((secure) ? "; secure" : "");
  173. }
  174.  
  175. function getCookie(name) {
  176. var cookie = " " + document.cookie;
  177. var search = " " + name + "=";
  178. var setStr = null;
  179. var offset = 0;
  180. var end = 0;
  181. if (cookie.length > 0) {
  182. offset = cookie.indexOf(search);
  183. if (offset != -1) {
  184. offset += search.length;
  185. end = cookie.indexOf(";", offset)
  186. if (end == -1) {
  187. end = cookie.length;
  188. }
  189. setStr = unescape(cookie.substring(offset, end));
  190. }
  191. }
  192. return(setStr);
  193. }
  194.  
  195. function str_replace(haystack, needle, replacement) {
  196. var temp = haystack.split(needle);
  197. return temp.join(replacement);
  198. }
  199.  
  200. function exists(id){
  201. q = getCookie('card');
  202. if(q&&q!='null'&&q.indexOf(id)!=-1)
  203. return true;
  204.  
  205. return false;
  206. }
  207.  
  208. $(document).ready(
  209. function() {
  210.  
  211. if ($.browser.msie && ($.browser.version == "6.0")) {
  212. $("div.footer-designer a").hover(
  213. function() {
  214. $(this).find("strong").addClass("hover");
  215. },
  216.  
  217. function() {
  218. $(this).find("strong").removeClass("hover");
  219. }
  220. );
  221. }
  222.  
  223. var curRel = 1;
  224. var curZIndex = 100;
  225. $("div.form-item select").each(
  226. function() {
  227. var curSelect = $(this);
  228. curSelect.parent().css("z-index", curZIndex--);
  229. // curSelect.attr("rel", curRel++).niceSelect();
  230. }
  231. );
  232.  
  233. $("div.catalogue-field select").each(
  234. function() {
  235. var curSelect = $(this);
  236. curSelect.parent().parent().css("z-index", curZIndex--);
  237. // curSelect.attr("rel", curRel++).niceSelect();
  238. }
  239. );
  240.  
  241. }
  242. );
  243.  
  244. function delCard(id){
  245.  
  246. if(exists(id)){
  247.  
  248. q = getCookie('card');
  249. if(q.indexOf('-')==-1)
  250. q = [q];
  251. else
  252. q = q.split('-');
  253. newC = [];
  254. for(i=0;i<q.length;i++){
  255.  
  256. if(q[i]!=id)
  257. newC[newC.length] = q[i];
  258. }
  259. setCookie('card',newC.join("-"),null,'/');
  260. setCookie(id,'',null,'/');
  261. window.location.replace( window.location.href);
  262. }
  263. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.