是否可以在 JavaScript 中创建一个在刷新页面时不会更改的变量?

发布于 2024-12-01 21:36:54 字数 1046 浏览 1 评论 0原文

在我问这个问题之后:

我在想关于此解决方案:

  • javascript 脚本内创建一个布尔标志并将其设置为 true
    • true表示使用浏览器刷新页面。
    • false表示使用按钮刷新页面。
  • 在按钮的 onclick 属性内,我将 flag 设置为 false 并调用 window.location.reload()刷新页面。
  • 标记的 onload 属性内,我检查 flag 是否为 true 切换到 Home 选项卡,否则切换到 Page1 选项卡,其中包含我想要刷新其内容的
    标签。之后,将flag设置为true。*

但不幸的是它不起作用,flag始终是 >true 当涉及 标记的 onload 属性时。是否有可能(以某种方式)在javascript中创建一个在刷新页面时不会改变的变量?

*我的页面由许多选项卡组成,第一个是“主页”(使用浏览器刷新页面时应显示该选项卡),第二个是“Page1”(其中包含

< /code> 标签我想刷新其内容以及使用按钮刷新页面时应显示的位置)

After I ask this question:

I was thinking about this solution:

  • Create a boolean flag inside the javascript script and make it true:
    • true indicates refreshing the page using the browser.
    • false indicates refreshing the page using a button.
  • Inside onclick attribute of the button, I set flag to false and call window.location.reload() to refresh the page.
  • Inside onload attribute of <body> tag, I check the flag if it's true switch to Home tab else switch to Page1 tab where it contains the <div> tag I want to refresh its contents. After that, set flag to true.*

But unfortunately it doesn't work and flag is always true when it comes to onload attribute of <body> tag. Is it possible (somehow) to create a variable, that doesn't change on refresh the page, in javascript?

*My page consistes of many tabs, first one is Home (where it should be shown when refreshing the page using the browser) and second one is Page1 (where it contains the <div> tag I want to refresh its contents and where it should be shown when refreshing the page using the button)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵你手 2024-12-08 21:36:54

浏览器是无状态的,这意味着每次加载页面时都会刷新页面上的所有内容。

有几种方法可以存储您可以在请求之间访问的持久信息:

The browser is stateless, which means that everything on the page is refreshed each time the page loads.

There are a few ways to store persistant information that you can access between requests:

  • Store the data you want in a cookie and read it when the page loads
  • Pass data to the URL and read the current URL (including the data that you passed) when the page loads. For example: Redirect the browser to http://something.com/yourpage.html#flag=true if the user refreshed through the browser or redirect to http://something.com/yourpage.html#flag=false if the user clicked the button. On the onload of the page just read the current URL and see what the flag is.
  • Use a cookie to create a session and store data on the server (technically this is very similar to the first option except you just store a token on the client and pass it to the server to retrieve data that was stored there)
楠木可依 2024-12-08 21:36:54

不可以。但是,您可以通过多种方式存储变量的值。

  • cookie 。
  • url(查询字符串)隐藏元素中的
  • 从会话变量设置的页面上

No. However, you can store the variable's value in a number of ways.

  • a cookie
  • in the url (query string)
  • hidden element on page set from a session variable.
嘿哥们儿 2024-12-08 21:36:54

这听起来像是您可以在浏览器中使用 sessionStorage 或 localStorage 的情况,如果浏览器不支持,您可以回退到 cookie...

This sounds like a situation where you could use sessionStorage or localStorage in the browser and if the browser doesn't support that, you could fallback on cookies...

千仐 2024-12-08 21:36:54

您可以使用 cookie、window.name 和 window.location.hash 键来保存数据

You can persist the data by using cookies, window.name, and window.location.hash key

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文