Tuesday 15 May 2012

javascript - Issues with Jquery.cookie plugin -



javascript - Issues with Jquery.cookie plugin -

i've researched several q&as , articles on subject , seems somewhere i'm making error in here. i've included code in $(document).ready javascript , loading @ end of index.html. both ids used inserted on html , looking through jquery.cookies documentation i'm not seeing i've forgotten if anything. should load cookie function @ origin of html?

html buttons switch proper language html file...

<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 liked 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 includes cookie code...

$(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 problem have, found problem in code.

first, don't need <div> in link.

<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 liked served?</h1> <a href="en.html" id="set_en_button" class="btn btn-primary btn-lg text-center">in english</a> </div>

second, click event function never work, should utilize e.preventdefault(); prevent loading new page before set cookie.

class="lang-js prettyprint-override">$(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'); }); });

javascript jquery html cookies

No comments:

Post a Comment