Using the javascript, jquery cookie

Asked 2 years ago, Updated 2 years ago, 86 views

Please teach me.What should I write and how?
What do you want to do?
1. Display a list of cookies on the page and
I would like to display a message if nothing has been saved yet.
(e.g. "Not saved yet")
2. I want the latest order to be displayed at the top,

Currently, if it has not been saved yet, the screen will be blank

Below is the list.html of the process to be displayed when the button is pressed on the registration screen.

<!DOCTYPE html>
<html lang="ja"><head>
<script src="/js/jquery.cookie.js" charset="euc-jp"></script>
<script type="text/javascript" charset="utf-8">
$(function(){ 
   $(document).ready(function(){
   vars = $.cookie('favorite');` 
    if(!(ss==true)){
      $('#list').text('Nothing clicked'');
    } else if(ss=$.cookie('favorite')){
      ss = JSON.parse(ss);
      for (vari=0;i<fa.length;i++) {
        ss[i] = ss[i].substring(ss[i].lastIndexOf('_0')+2, ss[i].length);
        vard=ss[i].split('_');
        $('#list').append($('<img src='+d[1]+'>'));
      };
    }
  });

</script>
</head>
<body class="index">
<divid="list">

</div>
</body>
</html>

Below is the basic registration image.html

<!doctype html>
<html lang="ja">
<script>
    $(function(){
        $(document).ready(function(){
        });

        $('img[src="/images/btn_favorite.png"]').click(function(){

            var index=$('img[src="/images/btn_favorite.png"]').index(this);
            var h2 = $('div.box_text:eq('+index+')').children('h2');
            varimg=$('div.box_main:eq('+index+')').find('img');
            var src_para = $(this).close('a') .attr('href');

            $.cookie.json=true;
            vardata=$(h2).text()+'_'+'./'+img.attr('src');
            if($.cookie('favorite')==null){
                varp = [ ];
                p.push(data);
                $.cookie('favorite', JSON.stringify(p), {'expires':7, 'path':'/'});
            }
            else{
                vars = JSON.parse($.cookie('favorite'));
                console.log(ss);
                if(ss.indexOf(data)==-1){
                    ss.push(data);
                }
                $.cookie('favorite', JSON.stringify(fa), {'expires':7, 'path':'/'});

            }
        });
    });
</script>
</head>
<body>

<div class="box_main">
<div class="photo">a href=">img src="images/A.jpg">/a>>>
<div class="box_text">
<h2> Accessories A</h2>
</div></div>
<table width="100%">
<tbody>
<tr>
<td width="50%">
<a href="class="etc_link"><img src="/images/btn_favorite.png">/a>
</td>
</tr>
</tbody>
</table>


<div class="box_main">
<div class="photo">a href=">img src="images/B.jpg">/a>>>
<div class="box_text">
<h2>Product B</h2>
</div></div>
<table width="100%">
<tbody>
<tr>
<td width="50%">
<a href="class="etc_link"><img src="/images/btn_favorite.png">/a>
</td>
</tr>
</tbody>
</table>


<div class="box_main">
<div class="photo">a href=">img src="images/C.jpg">/a>>>
<div class="box_text">
<h2>Product C</h2>
</div></div>
<table width="100%">
<tbody>
<tr>
<td width="50%">
<a href="class="etc_link"><img src="/images/btn_favorite.png">/a>
</td>
</tr>
</tbody>
</table>
</html>

javascript jquery cookie

2022-09-30 20:18

1 Answers

If jQuery handles cookies, deploy jQuery Cookie plugin.

This article will be helpful.
Handle cookies easily with the jQuery plug-in "jquery.cookie.js"

Also, do you want to do what you want to do in the second source?Or do you want to do the first and second content at the same time?

$(document).ready(... only calls one.Multiple $(document).ready(s) will not run

So, what do you want to do?

<script type="text/javascript" charset="utf-8">
$(function(){ 
  $(document).ready(function(){
    vars = $.cookie('favorite');
    if(ss===undefined){
      $('#list').text('Nothing has been saved yet');
    } else{ 
      fa = JSON.parse(ss);
      for (vari=0;i<ss.length;i++) {
        ss[i] = ss[i].substring(ss[i].lastIndexOf('_0')+2, ss[i].length);
        vard=ss[i].split('_');
        $('#list').append($('<img src='+d[1]+'>');
      };
    }
  });
});
</script>


2022-09-30 20:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.