Javascript 加载新html页面时JS变量是否已销毁?
只是一个简单的问题。当您有一个包含普通函数、变量和其他内容的js文件时,该页面将被加载,然后在需要代码时使用。但我的问题是,当您导航到另一个页面(例如使用window.location.href)时,您创建的任何/所有变量会发生什么变化?它们还存在于记忆中吗?它们是否仍然可以访问?或者,在重新加载原始文件之前,它们会永远消失吗Javascript 加载新html页面时JS变量是否已销毁?,javascript,variables,browser,Javascript,Variables,Browser,只是一个简单的问题。当您有一个包含普通函数、变量和其他内容的js文件时,该页面将被加载,然后在需要代码时使用。但我的问题是,当您导航到另一个页面(例如使用window.location.href)时,您创建的任何/所有变量会发生什么变化?它们还存在于记忆中吗?它们是否仍然可以访问?或者,在重新加载原始文件之前,它们会永远消失吗 干杯简短的回答:是 JS文件中存储的每个变量都将丢失,或者说,每次重新加载页面或更改Window.location.href. 要在重新加载之间持久保存数据或使用变量值,
干杯简短的回答:是 JS文件中存储的每个变量都将丢失,或者说,每次重新加载页面或更改
Window.location.href.
要在重新加载之间持久保存数据或使用变量值,应使用客户端数据存储,如sessionStorage
或localStorage
或cookies
例如,让我们假设使用可以使用的persist变量的值
let persist = "My data";
// Store it in localstorage
localStorage.set('persist', persist);
// Get the value of your saved variable to use it elsewhere
let persist = locastorage.get("persist");
//To delete the variable from storage
localStorage.removeItem('persist')
// To clear the entire storage
localStorage.clear()
语法与
sessionStorage
相同,但使用sessionStorage
存储的值仅在页面会话期间可用。有关DOM存储的更多信息,请阅读。简短回答:是
JS文件中存储的每个变量都将丢失,或者说,每次重新加载页面或更改Window.location.href.
要在重新加载之间持久保存数据或使用变量值,应使用客户端数据存储,如sessionStorage
或localStorage
或cookies
例如,让我们假设使用可以使用的persist变量的值
let persist = "My data";
// Store it in localstorage
localStorage.set('persist', persist);
// Get the value of your saved variable to use it elsewhere
let persist = locastorage.get("persist");
//To delete the variable from storage
localStorage.removeItem('persist')
// To clear the entire storage
localStorage.clear()
语法与
sessionStorage
相同,但使用sessionStorage
存储的值仅在页面会话期间可用。有关DOM存储的更多信息,请阅读。是的,它们已销毁。是的,它们已销毁。