javascript - How can I traverse through cookie and then upon clicking a button change the url depending on values from cookie -


i have cookie dataid giving data below "d_2781467,d_2792290,d_2803725,d_2677313,d_2799569,d_2805134,d_2758142,d_2802506,d_2802509,d_2802508,d_2803726,d_2652515"

and in body have valies below

<body class="mycars" data-listing-id="2792290"> 

and url like

http://www.abcd.com/c_f_s/d_2792290/xyz.html

what want in page upon clicking button , user taken next url in

http://www.abcd.com/c_f_s/d_2803725

so somehow need traverse through cookie , index , on pressing button change url number received cookie

you can like,

html

<a href="javascript:;" id="next">next</a> 

script

$(function(){     var dataarr = ['d_2781467', 'd_2792290', 'd_2803725', 'd_2677313', 'd_2799569',                    'd_2805134', 'd_2758142', 'd_2802506', 'd_2802509','d_2802508',                    'd_2803726', 'd_2652515'];     var index = 0;     $.cookie('index', 0);      $('#next').on('click', function (e) {           e.preventdefault();         if (index < dataarr.length-1) { // check index length             index++ ;// increment index         } else {             index = 0; // reset index                      }         var currentindex = $.cookie('index'); // current cookie index cookie         $.cookie('index', index);                 alert('http://www.abcd.com/c_f_s/' + dataarr[currentindex]);     }); }); 

you can use cookie plugin

live demo


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -