Javascript 在MTurk HIT中设置cookie
我最近在一次MTurk点击中遇到了使用javascript cookies的问题。特别是,我正在尝试跟踪用户偏好,w.r.t显示/隐藏HIT指令 到目前为止,我的做法如下:Javascript 在MTurk HIT中设置cookie,javascript,cookies,plotly-dash,mturk,Javascript,Cookies,Plotly Dash,Mturk,我最近在一次MTurk点击中遇到了使用javascript cookies的问题。特别是,我正在尝试跟踪用户偏好,w.r.t显示/隐藏HIT指令 到目前为止,我的做法如下: <body> <div id='instructionButton'> <!-- Button triggering instruction body to collapse/show --> </div> <div id='ins
<body>
<div id='instructionButton'>
<!-- Button triggering instruction body to collapse/show -->
</div>
<div id='instructionBody'>
<!-- Instruction content (collapsible) -->
...
</div>
</body>
<script>
const instructionBodyId = 'instructionBody';
const instructionButtonId = 'instructionButton';
const cookieName = 'my_cookie_name';
var isInstructionShown = true;
var instrContent = $('#' + instructionBodyId);
var instrButton = $('#' + instructionButtonId);
function setCookie(name, value) {
var date = new Date();
<!-- Cookie valid for 48h -->
date.setTime(date.getTime() + (48 * 60 * 60 * 1000));
var expires = "; expires=" + date.toUTCString();
document.cookie = name + "=" + value + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
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, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function toggleInstructions(isShow) {
setCookie(cookieName, isShow);
isInstructionShown = isShow;
if (isShow) {
instrContent.slideDown();
} else {
instrContent.slideUp();
}
}
function prepare_cookie() {
instrButton.click(function() {
toggleInstructions(!isInstructionShown);
});
let cookieVal = getCookie(cookieName);
if (cookieVal == "false") {
toggleInstructions(false);
} else {
toggleInstructions(true);
}
}
$(document).ready(function() {
prepare_cookie();
});
</script>
...
const instructionBody id='instructionBody';
const instructionButtonId='instructionButton';
const cookieName='my_cookie_name';
var isInstructionShown=真;
var instrContent=$(“#”+指令主体ID);
var instrButton=$(“#”+指令按钮id);
函数setCookie(名称、值){
变量日期=新日期();
date.setTime(date.getTime()+(48*60*60*1000));
var expires=“;expires=“+date.toutString();
document.cookie=name+“=”+value+expires+“path=/”;
}
函数getCookie(名称){
变量nameEQ=name+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i
上面的代码显示了我正在创建的命中布局的一部分,当在MTurk中直接编辑命中时进行测试时,cookie按预期工作(它显示在Google Chrome中,并按预期工作,自动显示/隐藏指令)
不幸的是,当发布点击时,cookie似乎没有被设置(它没有出现在Google Chrome中显示的cookie列表中)