如何使用JavaScript将变量的值传递给下一页?

时间:2014-11-30 04:40:34

标签: javascript html

我希望能够将DIHA传递到下一页。我在该页面上的代码需要能够访问其值。这是我到目前为止所尝试的内容:

function tally() {
    var Dpoint, Ipoint, Hpoint, Apoint;

    var party_Score = ['Dpoint', 'Ipoint', 'Hpoint', 'Apoint'];
    var D, I, H, A;
    var value_Point;
    var type_Choice;
    var tag_Choice;

    var inputs = document.getElementsByTagName("input"),
        iLength = inputs.length;

    D = I = H = A = 0;

    for (i = 0; i < iLength; i++) if (inputs[i].checked) {
        value_Point = parseInt(inputs[i].value);
        if (inputs[i].name.search('D') > -1){ D += value_Point; }
        if (inputs[i].name.search('I') > -1){ I += value_Point; }
        if (inputs[i].name.search('H') > -1){ H += value_Point; }
        if (inputs[i].name.search('A') > -1){ A += value_Point; }
    }

    // DIHA is areadly added how to pass them them to other page ?
    document.getElementById('D').style.width = D + 'px';
    alert(D);
    document.getElementById('I').style.width = I + 'px';
    document.getElementById('H').style.width = H + 'px';
    document.getElementById('A').style.width = A + 'px';
    Dpoint = D; //passing this value to next page
    Ipoint = I; //passing this value to next page
    Hpoint = H; //passing this value to next page
    Apoint = A; //passing this value to next page
}

1 个答案:

答案 0 :(得分:1)

在test1.html中你写

localStorage.setItem("Dpoint", D); //passing this value to next page
localStorage.setItem("Ipoint", I); //passing this value to next page
localStorage.setItem("Hpoint", H); //passing this value to next page
localStorage.setItem("Apoint", A); //passing this value to next page

并在test2.html中获得类似这样的值

D = localStorage.getItem("Dpoint"); 
I = localStorage.getItem("Ipoint");
H = localStorage.getItem("Hpoint"); 
A = localStorage.getItem("Apoint"); 

注意:这仅适用于支持HTML5本地存储的现代浏览器。为了使其更强大并适用于所有浏览器,您可以使用一些库,例如YUI,它有一个名为cache-offline的模块。如果您想自己实现它,可以查看YUI代码here

安全立场,localstorage和Cookie比URL GET参数更安全,因为它们强制执行相同的域限制。如果您的数据更敏感,那么您可能希望将其视为以某种加密格式存储在服务器端。

持久性角度来看,此方法仅在浏览器中保留数据。如果您想要跨浏览器持久性,那么您需要再次考虑将信息存储在服务器中。