我试图让以下内容开始:
我有一个下拉列表,它应该更改脚本文件并根据droplist选项重新执行它。它还需要将选择变量存储在cookie中,因此在下次访问时,将在加载页面时启动正确的脚本。可以随时更改下拉列表(包括cookie)以再次更改脚本。
我试图编写脚本,但脚本不起作用。修复此脚本的一些帮助将不胜感激。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
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) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie()
{
var user=getCookie("droplist-keuze");
if (user != "")
{
loadScript("script'+user+'.js", function());
} else
{
loadScript("script.js", function());
}
}
</script>
<script>
function loadJS(v){
var d=document, h=d.getElementsByTagName('head')[0], newScript;
try {h.removeChild(d.getElementById('lib_lang'));} catch (e){}
newScript = d.createElement('script');
newScript.id = 'lib_lang';
newScript.type = 'text/javascript';
newScript.src = 'script'+v+'.js'; // change your path here
h.appendChild(newScript);
setCookie("droplist-keuze", v, 30;
}
</script>
</head>
<body onload="checkCookie()">
<select lang="en" onchange="loadJS(this.value)">
<option selected="selected" disabled="true"> Select</option>
<option value="1">1 </option>
<option value="2">2 </option>
<option value="3">3 </option>
<option value="4">4 </option>
</select>
</body>
</html>