我正在调用location.reload(),同时选择用于更改语言的下拉选项
<html>
<head>
<script type="text/javascript">
function changeLanguage(){
location.reload();
}
function loadPage() {
var language = document.getElementById("lang").value;
alert(language);
}
</script>
</head>
<body onload="loadPage()">
<select name="lang" id="lang" onchange="changeLanguage()">
<option value="english">English</option>
<option value="hindi">हिंदी</option>
</select>
</body>
</html>
如果我在Firefox中运行此页面,它会提醒我选择哪一个。这就是我要的。但是,如果我在Google Chrome中运行此页面,它会始终提供第一个选项。
我认为在加载时,下拉列表会重新加载,因此获取选定值是第一选择。
实际上我想要选择一个偶数页面加载。
我已经将这个概念用于不同的目的,为了简化我已经返回这个简单的代码。
答案 0 :(得分:1)
我通过添加Cookie解决了我的问题:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function changeLanguage(lang){
setCookie("flip_book_lang", lang, 7);
location.reload();
}
function loadPage(lang) {
var language = document.getElementById("lang").value;
var curr_lang = getCookie("flip_book_lang");
document.getElementById("lang").value = curr_lang;
alert(curr_lang);
}
//Source : w3schools, 023-09-2014 http://www.w3schools.com/js/js_cookies.asp
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1){
return c.substring(name.length,c.length);
}
}
return "";
}
function removeCookie(cname){
document.cookie = cname + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
</script>
</head>
<body onload="loadPage()">
<select name="lang" id="lang" onchange="changeLanguage(this.value)">
<option value="english">English</option>
<option value="hindi">हिंदी</option>
</select>
</body>
</html>