设置Javascript cookie并检索它们

时间:2016-06-10 12:30:44

标签: javascript php css3

我希望有一个DIV可以在屏幕上拖动并保存位置,以便下次返回时,DIV在同一个地方。

我已经使用javascript / CSS并将位置值存储到javascript cookie中,然后在重新加载页面时检索(下面的代码)

我的问题是我有一个页面,根据URL中的变量,内容被加载到屏幕的不同部分

即。 www.example.com/example.php?date=10062016可能会在屏幕左侧填充内容,而www.example.com/example.php?date=11062016可能会在屏幕右侧显示内容。 因此,如果我将DIV拖到屏幕的右侧,则内容将覆盖在一个页面上,而不是另一个页面上。

我认为我需要做的是将cookie名称保存为php变量的值(日期),并使用名称作为变量加载cookie。我相信这将允许我存储多个cookie,这将保存不同日期的不同位置。

我还注释了我认为可能在底部工作的JavaScript

function drag_start(event) {
    var style = window.getComputedStyle(event.target, null);
    event.dataTransfer.setData("text/plain",
    (parseInt(style.getPropertyValue("left"),10) - event.clientX) + ',' + (parseInt(style.getPropertyValue("top"),10) - event.clientY));
} 
function drag_over(event) { 
    event.preventDefault(); 
    return false; 
} 
function drop(event) { 
    var offset = event.dataTransfer.getData("text/plain").split(',');
    var dm = document.getElementById('dragme');
    dm.style.left = (event.clientX + parseInt(offset[0],10)) + 'px';
    dm.style.top = (event.clientY + parseInt(offset[1],10)) + 'px';
    var pos = {left: dm.style.left, top: dm.style.top};
    document.cookie = JSON.stringify(pos);
    event.preventDefault();
    return false;
}
var dm = document.getElementById('dragme'); 
try {
  var pos = JSON.parse(document.cookie);
  dm.style.left = pos.left ? pos.left : '0px';
  dm.style.top = pos.top ? pos.top : '0px';
} catch (e) {
  // Some error handling
}
dm.addEventListener('dragstart',drag_start,false); 
document.body.addEventListener('dragover',drag_over,false); 
document.body.addEventListener('drop',drop,false); 


/*

function drop(event) { 
    var offset = event.dataTransfer.getData("text/plain").split(',');
    var dm = document.getElementById('dragme');
    dm.style.left = (event.clientX + parseInt(offset[0],10)) + 'px';
    dm.style.top = (event.clientY + parseInt(offset[1],10)) + 'px';
    var pos = {left: dm.style.left, top: dm.style.top};
    var str = JSON.stringify(pos);
    var dat = <?php echo $_GET[date];?>;
    document.cookie = dat+"="+str;
    event.preventDefault();
    return false;
}
var dm = document.getElementById('dragme'); 
try {
  var pos = JSON.parse(dat);
  dm.style.left = pos.left ? pos.left : '0px';
  dm.style.top = pos.top ? pos.top : '0px';
} catch (e) {
  // Some error handling
}
*/
aside { 
    position:  absolute;
    left: 0;
    top: 0; /* set these so Chrome doesn't return 'auto' from getComputedStyle */
    width: 200px; 
    background: rgba(255,255,255,0.66); 
    border: 2px  solid rgba(0,0,0,0.5); 
    border-radius: 4px; padding: 8px;
}
<aside draggable="true" id="dragme">
    This is an aside, drag me.
</aside>
<p>I never am really satisfied that I understand anything; because, understand it well as I may, my comprehension can only be an infinitesimal fraction of all I want to understand about the many connections and relations which occur to me, how the matter in question was first thought of or arrived at, etc., etc.</p>
<p>In almost every computation a great variety of arrangements for the succession of the processes is possible, and various considerations must influence the selections amongst them for the purposes of a calculating engine. One essential object is to choose that arrangement which shall tend to reduce to a minimum the time necessary for completing the calculation.</p>
<p>Many persons who are not conversant with mathematical studies imagine that because the business of [Babbage’s Analytical Engine] is to give its results in numerical notation, the nature of its processes must consequently be arithmetical and numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine its numerical quantities exactly as if they were letters or any other general symbols; and in fact it might bring out its results in algebraical notation, were provisions made accordingly.</p>
<p>The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform. It can follow analysis, but it has no power of anticipating any analytical revelations or truths. Its province is to assist us in making available what we are already acquainted with.</p>

1 个答案:

答案 0 :(得分:1)

我的javascript代码中只更改了2行。我已使用localStorage方法setItem()getItem()替换了设置和获取Cookie数据。

关于localStorage保存数据的时间,您可以更好地结帐@Pointy's answer here。 简而言之,目前还不清楚何时清除此数据(通常在浏览器中保留足够长的时间),但您可以随时使用localStorage.setItem()和{{1}清除或更新该数据方法。

localStorage.clearItem()