简体   繁体   English

Jquery.cookie插件的问题

[英]Issues with Jquery.cookie plugin

I've researched several Q&As and articles on the subject and it seems somewhere I'm making a mistake in here. 我研究了有关该主题的一些问答和文章,似乎我在这里犯了一个错误。 I've included this code in my $(document).ready javascript and loading this at the end of my index.html. 我已将此代码包含在$(document).ready javascript中,并将其加载到index.html的末尾。 Both IDs used are properly inserted on the HTML and looking through the Jquery.cookies documentation I'm not seeing what I've forgotten if anything. 所使用的两个ID均已正确插入HTML并浏览Jquery.cookies文档,但看不到遗忘的内容。 Would it be that I should load the cookie function at the beginning of my HTML? 是否应该在HTML的开头加载cookie函数?

HTML where the buttons switch to the proper language HTML file... 按钮可切换到正确语言的HTML HTML文件...

<div class="col-xs-12 col-sm-6 col-md-6"><br>
        <h1>Comment aimeriez vous être servi?</h1><a href="fr.html">
          <div id="set_fr_button" class="btn btn-primary btn-lg text-center">En Français</div></a>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6"><br>
        <h1>How would you liked to be served?</h1><a href="en.html">
          <div id="set_en_button" class="btn btn-primary btn-lg text-center">In English</div></a>
      </div>

JS file which includes the cookie code... 包含cookie代码的JS文件...

        $(function () {

    var url = 'mannydesigns.co';
    var en_page = 'en.html';
    var fr_page = 'fr.html';

    if ($.cookie('default_page') != null) {
        if (window.location.href != url + '/' + $.cookie('default_page')) {
            window.location.href = url + '/' + $.cookie('default_page');
        }
    }

    $('#set_en_button').click(function () {
        $.cookie('default_page', en_page, { expires: 999 });
    });

    $('#set_fr_button').click(function () {
        $.cookie('default_page', fr_page, { expires: 999 });
    });

    });

Not sure what problem you have, but I found some problem in your code. 不知道您遇到什么问题,但是我在您的代码中发现了一些问题。

First, you don't need <div> in link. 首先,您不需要链接中的<div>

<div class="col-xs-12 col-sm-6 col-md-6"><br>
    <h1>Comment aimeriez vous être servi?</h1>
    <a href="fr.html" id="set_fr_button" class="btn btn-primary btn-lg text-center">En Français</a>
</div>
<div class="col-xs-12 col-sm-6 col-md-6"><br>
    <h1>How would you liked to be served?</h1>
    <a href="en.html" id="set_en_button" class="btn btn-primary btn-lg text-center">In English</a>
</div>

Second, your click event function will never work, you should use e.preventDefault(); 其次,您的点击事件功能将永远无法使用,您应该使用e.preventDefault(); to prevent loading new page before set cookie. 以防止在设置Cookie之前加载新页面。

$(function () {
    var url = 'mannydesigns.co';
    var en_page = 'en.html';
    var fr_page = 'fr.html';

    if ($.cookie('default_page') != null) {
        if (window.location.href != url + '/' + $.cookie('default_page')) {
            window.location.href = url + '/' + $.cookie('default_page');
        }
    }

    $('#set_en_button').click(function (e) {
        e.preventDefault();
        $.cookie('default_page', en_page, { expires: 999 });
        window.location.href = $(this).attr('href');
    });

    $('#set_fr_button').click(function (e) {
        e.preventDefault();
        $.cookie('default_page', fr_page, { expires: 999 });
        window.location.href = $(this).attr('href');
    });
});

Well folks what worked for me was isolating this code and loading it last and when it worked I realized that also the code was adding the URL when I'm already on the home page and therefore couldn't find the right HTML page...so the final code that worked is the following (basically the same, removing the url in my if statement, no e.preventdefault as it's not needed): 伙计们,对我有用的是隔离此代码并最后加载它,当它起作用时,我意识到当我已经在主页上时该代码也在添加URL,因此找不到正确的HTML页面...因此,有效的最终代码如下(基本上是相同的,删除了我的if语句中的url,没有e.preventdefault,因为不需要它):

  $(function () {

var url = 'mannydesigns.co';
var en_page = 'en.html';
var fr_page = 'fr.html';

if ($.cookie('default_page') != null) {
    if (window.location.href != '/' + $.cookie('default_page')) {
        window.location.href = '/' + $.cookie('default_page');
    }
}

$('#set_en_button').click(function () {
    $.cookie('default_page', en_page, { expires: 999 });
});

$('#set_fr_button').click(function () {
    $.cookie('default_page', fr_page, { expires: 999 });
});

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM