问题保存设置和检索cookie

时间:2017-06-14 17:25:54

标签: javascript jquery cookies

从url查询我试图获取两个变量和值。 all_links all_ers 我的目标是从网址中检索值,设置Cookie并在用户转到我网站上的表单时检索它们。我做了一个浏览器检查,我没有看到任何cookie。我还应该考虑在jQuery cookie上使用javascript吗?

<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js'></script>
<script>
// Parse the URL
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Give the URL parameters variable names
var all_links = getParameterByName('link1');
var all_ers = getParameterByName('link2');

// Set the cookies
$.cookie('link1', all_links);
$.cookie('link2', all_ers);

// Grab the cookie value and set the form field values
$(document).ready(function(){
    $('input[name=link1').val(all_links);
    $('input[name=link2').val(all_ers);
})
</script>

0 个答案:

没有答案