Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从一个页面访问另一个页面的javascript变量?_Javascript - Fatal编程技术网

如何从一个页面访问另一个页面的javascript变量?

如何从一个页面访问另一个页面的javascript变量?,javascript,Javascript,我试图访问从一个页面到另一个页面声明的全局变量。我只想从另一个页面重置变量的值,其中第二个页面在新选项卡中打开,或者可以手动打开 以下是主页上的javascript代码: <script type="text/javascript"> var IDLE_TIMEOUT = 10; //seconds var _idleSecondsCounter = 0; document.onclick = function() {

我试图访问从一个页面到另一个页面声明的全局变量。我只想从另一个页面重置变量的值,其中第二个页面在新选项卡中打开,或者可以手动打开

以下是主页上的javascript代码:

<script type="text/javascript">

       var IDLE_TIMEOUT = 10; //seconds
       var _idleSecondsCounter = 0;

       document.onclick = function() {
            _idleSecondsCounter = 0;
        };

        document.onmousemove = function() {
            _idleSecondsCounter = 0;
        };

        document.onkeypress = function() {
            _idleSecondsCounter = 0;
        };

</script>

var IDLE_超时=10//秒
var_idleSecondsCenter=0;
document.onclick=函数(){
_IDLESecondsCenter=0;
};
document.onmousemove=函数(){
_IDLESecondsCenter=0;
};
document.onkeypress=函数(){
_IDLESecondsCenter=0;
};
这是我在另一个页面中尝试重置“\u idleSecondsCenter”变量值时使用的javascript代码

<script type="text/javascript">

        document.onclick = function() {

            _idleSecondsCounter = 0;

        };

        document.onmousemove = function() {

            _idleSecondsCounter = 0;

        };

        document.onkeypress = function() {

            _idleSecondsCounter = 0;

        };

</script>

document.onclick=函数(){
_IDLESecondsCenter=0;
};
document.onmousemove=函数(){
_IDLESecondsCenter=0;
};
document.onkeypress=函数(){
_IDLESecondsCenter=0;
};

有可能吗?

如果你不想实时交流,那是不可能的。页面脚本的每次重新加载都将重置

尝试使用
localStorage

像这样试试

// Set value in localStorage
window.localStorage.setItem("myInfo","Demo data");

// Get value from locaStorage
var getData=window.localStorage.getItem("myInfo");

您可以使用以下方法之一解决问题:

客户端Cookie:

创建一个新对象,该对象具有get和set公共方法。Get将从cookie中读取值,set将在cookie中设置值。不要将此值存储在JavaScript变量中。此方法的缺点:如果cookie值被其他选项卡或窗口更改,则不会生成实时通知。作为一种解决方法,可以使用JavaScript计时器进行定期读取

服务器发送事件、长轮询或WebSocket:


根据您的目标浏览器,您必须选择上述一种或多种技术的组合,将值存储在服务器上,并向其他浏览器窗口或选项卡发送通知。如果您的后端是Asp.Net,那么SignalR是执行此任务的一个优秀工具。长轮询是所有主流浏览器都支持的唯一轮询。WebSockets/Server-Sent事件在旧浏览器中的支持有限。

正如其他人所提到的,变量不能从一页转到另一页。您必须使用另一种解决方案来在页面之间保留一些值

下面使用其他人建议的localStorage的解决方案适用于您的特定示例

主页

<script type="text/javascript">

    var IDLE_TIMEOUT = 10; //seconds

    //stores or overwrites the variable in local storage
    window.localStorage.setItem('_idleSecondsCounter', 0);

    document.onclick = function() {
        window.localStorage.setItem('_idleSecondsCounter', 0);
    };

    document.onmousemove = function() {
        window.localStorage.setItem('_idleSecondsCounter', 0);
    };

    document.onkeypress = function() {
        window.localStorage.setItem('_idleSecondsCounter', 0);
    };

</script>

var IDLE_超时=10//秒
//在本地存储器中存储或覆盖变量
window.localStorage.setItem(“IdleSecondsCenter”,0);
document.onclick=函数(){
window.localStorage.setItem(“IdleSecondsCenter”,0);
};
document.onmousemove=函数(){
window.localStorage.setItem(“IdleSecondsCenter”,0);
};
document.onkeypress=函数(){
window.localStorage.setItem(“IdleSecondsCenter”,0);
};
其他页面

<script type="text/javascript">

    document.onclick = function() {
        window.localStorage.setItem('_idleSecondsCounter', 0);
    };

    document.onmousemove = function() {
        window.localStorage.setItem('_idleSecondsCounter', 0);
    };

    document.onkeypress = function() {
        window.localStorage.setItem('_idleSecondsCounter', 0);
    };

</script>

document.onclick=函数(){
window.localStorage.setItem(“IdleSecondsCenter”,0);
};
document.onmousemove=函数(){
window.localStorage.setItem(“IdleSecondsCenter”,0);
};
document.onkeypress=函数(){
window.localStorage.setItem(“IdleSecondsCenter”,0);
};
要获取值并将其放入变量,可以执行以下操作

<script type="text/javascript">

    //retrieves value and puts it into the variable someNewVariableToStoreItIn
    var someNewVariableToStoreItIn = window.localStorage.getItem('_idleSecondsCounter');

    //parse it if you want an integer because localStorage saves things as strings
    someNewVariableToStoreItIn = parseInt(someNewVariableToStoreItIn);

</script>

//检索值并将其放入变量someNewVariableToStoreItIn中
var someNewVariableToStoreItIn=window.localStorage.getItem(“IdleSecondsCenter”);
//如果需要整数,请解析它,因为localStorage会将内容保存为字符串
someNewVariableToStoreItIn=parseInt(someNewVariableToStoreItIn);

这是不可能的,每次页面刷新脚本时,一个解决方案可能是
本地存储
Ok..ahm但是本地存储是如何使用的?这个问题必须问多少次?4重复:本地存储、查询字符串、cookie、服务器端存储+客户端/服务器查询。服务器变量不是选项?HTML5Web存储怎么样?我能把本地存储值传递给一个变量吗,先生?请详细说明一下??变量名='Nicholas';var setName=window.localStorage.setItem('name',name);var getName=window.localStorage.getItem('name');//getName='Nicholas'我不认为这是不可能的。我会问你为什么要这么做。听起来您希望在不同选项卡中的页面之间进行实时通信,这可以通过类似于库的套接字来完成。io@AnikIslamAbhi...I也就是说,在主页中设置本地存储的值之后,我可以将其传递给变量“\u idleSecondsCenter”吗?