从浏览器中获取节省的价值 [英] Get saved value from browsers

查看:141
本文介绍了从浏览器中获取节省的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用此脚本从jquery,Im中的浏览器中获取保存的价值.

How to get saved value from browsers in jquery,Im Using this script.

require(['jquery', 'jquery/jquery.cookie', 'jquery/ui'], function($){

    setTimeout(function(){
      console.log($('input#email').val());

        var subjectLength = $('#email').val().length;
        if(subjectLength > 0) {
            console.log('Value Available');
        } else {
            console.log('Value not  Available');
        }


    }, 3000);

});

推荐答案

您似乎正在尝试通过Magento中的JavaScript写入或更新Cookie.您也可以使用Session或Cookies使用PHP进行此操作.因为您说的是来自浏览器",所以我假设您想要一个JavaScript解决方案.

It looks like you are trying to write to or update a cookie via JavaScript in Magento. You could also do this with PHP using Session or Cookies too. Since you said 'from browser' I am assuming you want a JavaScript solution.

基本上,您将具有Setter和Getter函数来设置cookie的名称,值和有效期,然后具有从专门命名的cookie中获取值的函数.有时您也可能具有清除或删除功能,该功能基本上会将cookie设置为过去过期.

Basically, you will have a Setter and Getter function to set the name, value, and expiration of a cookie and then a function to get the value from a specifically named cookie. Sometimes you might have a clear or delete function too that basically sets he cookie to expire in the past.

我发现以下内容将对您有所帮助: https://magento. stackexchange.com/questions/163345/magento-2-how-to-use-cookie

I found the following which will help you: https://magento.stackexchange.com/questions/163345/magento-2-how-to-use-cookie

require(['jquery', 'jquery/jquery.cookie', 'jquery/ui'], function($){
  setTimeout(function(){
    console.log($('input#email').val());
    var subject = $('#email').val();
    var date = new Date();
    var minutes = 60;
    date.setTime(date.getTime() + (minutes * 60 * 1000));
    if($.cookie('subject').length) {
      console.log('Updating Cookie Value: "subject", "' + subject + '"');  
      $.cookie('subject', subject, {path: '/', expires: date});
    } else {
      console.log('Setting Cookie Value: "subject", "' + subject + '"');  
      $.cookie('subject', subject, {path: '/', expires: date});
    }
  }, 3000);
});

如果您打算做很多事情,可以使用自己的功能对此进行扩展.

You can expand on this with your own functions if you plan to do it a lot.

function setCookie(k, v, e){
  var check_cookie = $.cookie(k); // Get Cookie Value
  var date = new Date();
  var minutes = e || 60;
  date.setTime(date.getTime() + (minutes * 60 * 1000));
  if(check_cookie.length){
    $.cookie(k, '', {path: '/', expires: -1});
  }
  $.cookie(k, v, {path: '/', expires: date});
}

function getCookie(k){
  return $.cookie(k);
}

function deleteCookie(k){
  $.cookie(k, '', {path: '/', expires: -1});
}

希望有帮助.

这篇关于从浏览器中获取节省的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆