简体   繁体   English

JavaScript文件顶部的全局变量

[英]Global variable at top of the JavaScript file

Say I declare a global JavaScript variable at the top of a JavaScript file to equal 0 假设我在JavaScript文件的顶部声明了一个全局JavaScript变量,该变量等于0

ie

var jVar=0; 

Then I call a function in the JavaScript to set that variable to 1 ie. 然后,我在JavaScript中调用一个函数以将该变量设置为1,即。

function setVarToOne(){
    jVar=1;
}

If I then reload the page what will the variable equal before the setVarToOne function is called ? 如果然后重新加载页面,则在调用setVarToOne函数之前,变量将等于什么?

Will the jVar keep the value it was set to in the function or will it be re-initialised to 0? jVar保留在函数中设置的值还是将其重新初始化为0?

What I need to understand is, what happens to JavaScript variables when the page is reloaded? 我需要了解的是,重新加载页面后JavaScript变量会如何处理?

Thanks 谢谢

When you reload the page, everything in JavaScript will be lost and the code will execute like when you load the page for the first time. 重新加载页面时,JavaScript中的所有内容都会丢失,并且代码将像第一次加载页面时一样执行。

That means, your variable will be initialized to 0, and when the function is called, the variable will be set to 1. 这意味着,您的变量将被初始化为0,并且在调用函数时,变量将被设置为1。

When you reload the page the jVar will be set to 0 ; 当您重新加载页面时, jVar将被设置为0 because you are initializing it at very first level var jVar=0; 因为您是在第一级初始化的,所以var jVar=0; As your function is not called during reload. 由于您的函数在重新加载期间不会被调用。 If you make a call to setVarToOne() then and only then your global variable value will get change. 如果仅调用setVarToOne()则只有在此之后,全局变量值才会更改。

You are also having options to set the value at localStorage 您还可以选择在localStorage上设置值

localStorage.setItem(id, value);
var jVar=0;
document.write(jVar+'<br>');

function setVarToOne(){
    jVar=1;
}

setVarToOne();
document.write(jVar);

result: 1°document.write: 0 2°document.write: 1 结果:1°document.write:0 2°document.write:1

every time you load/reload the page, the variable 'jVar' will be set to 0 then to 1 每次加载/重新加载页面时,变量“ jVar”将设置为0,然后设置为1

jsfiddle jsfiddle

Your variable will be set to 0. 您的变量将设置为0。

When you reload the page the whole script start running again from the beginning - thus running var jVar=0 first. 重新加载页面时,整个脚本将从头开始再次运行-因此首先运行var jVar=0

If you need to save your data you can use local storage, session storage, cookies or persist it on a server. 如果需要保存数据,则可以使用本地存储,会话存储,cookie或将其持久保存在服务器上。

Take a look at this question that talks about persisting variables between page loads. 看一下这个有关页面加载之间的持久变量的问题。

It will be 0. 它将是0。

Javascript can't persist state in between 2 page loads by default. 默认情况下,Javascript无法在两次页面加载之间保持状态。 You could try first saving and then reading data from some store. 您可以尝试先保存,然后再从某些商店读取数据。 Either by API call or by saving it to localStorage, cookie or some other data storage. 通过API调用或将其保存到localStorage,Cookie或其他数据存储中。

Global variables are variables which will be stored throughout the Session. 全局变量是将在整个会话中存储的变量。 When you want to store something beyond the session use PHP. 当您想存储超出会话的内容时,请使用PHP。 You need PHP for the global variable to be stored. 您需要PHP来存储全局变量。 Copy your code and put it in a php file. 复制您的代码并将其放在php文件中。 In this file write this, 在这个文件中写下这个,

<?php
session_start();
$_SESSION['JVar'] = 0;
function setVarToOne(){
    $_SESSION['jVar'] = 1;
}

the variable will be stored in session all you have to do is pass that value to js by 该变量将存储在会话中,您所要做的就是通过将该值传递给js

echo "<script>var JVar = $_SESSION['jVar']</script>";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM