下面的代码检查 cookie 是否存在,如果不存在则设置它们。然后用户可以触发点击事件以存储新的 cookie。此代码用于在我自己的自定义 Google 翻译功能中切换语言。
使用location.reload();
如下所示时,它会正确加载 cookie 值,但语言不会改变。当我重新编码而不使用location.reload()
. 我宁愿重新加载页面。
我在这里缺少什么吗?
<script>
jQuery( document ).ready(function($) {
//Click event functionality for drop-down and flag language display
$("a.nturl").click(function() {
$.cookie("language_name", $(this).attr("title"),{ path: "/" });
$.cookie("flag_url", $(this).find("img").attr("src"),{ path: "/" });
location.reload();
});
$("a.flag").click(function() {
$.cookie("language_name", $(this).attr("title"),{ path: "/" });
$.cookie("flag_url", $(this).find("img").attr("src"),{ path: "/" });
location.reload();
});
$(function() {
var language_name = $.cookie("language_name");
var flag_url = $.cookie("flag_url");
if(typeof(language_name) == "undefined" || typeof language_name == "object") {
//alert("hello");
$.cookie("language_name","English");
var language_name = $.cookie("language_name");
}
if(typeof(flag_url) == "undefined" || typeof flag_url == "object") {
$.cookie("flag_url","<?php echo plugins_url(); ?>/google-language-translator/images/flags24/unitedstates.png");
var flag_url = $.cookie("flag_url");
}
$("div.selected").html( "<a class=\"notranslate nturl\" onclick=\"return false;\" href=\"#\"><span class=\"flag\"><img class=\"flagimg flagselect\" src=\"" + flag_url + "\" alt=\"" + language_name + "\" height=\"16\" width=\"16\"></span>" + language_name + "</a>");
$.removeCookie("language_name");
$.removeCookie("flag_url");
});
$(function() {
var language_name = $.cookie("language_name");
var flag_url = $.cookie("flag_url");
var language_name = $.cookie('language_name');
if (language_name == "English") {
$('.en').css('display','none');
$('.selected').click(function() { $('.en').css('display','none'); });
}
if (language_name == "Spanish") {
//alert(language_name);
$('.selected').click(function() { $('.es').css('display','none'); });
}
$("div.selected").html( "<a class=\"notranslate nturl\" onclick=\"return false;\" href=\"#\"><span class=\"flag\"><img class=\"flagimg flagselect\" src=\"" + flag_url + "\" alt=\"" + language_name + "\" height=\"16\" width=\"16\"></span>" + language_name + "</a>");
});
});