有没有可能创建一个变量,这不会改变刷新页面,在JavaScript中? [英] Is it possible to create a variable, that doesn't change on refresh the page, in javascript?

查看:146
本文介绍了有没有可能创建一个变量,这不会改变刷新页面,在JavaScript中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我询问这个​​ 问题



我正在考虑这个解决方案:


  • javascript 脚本中创建一个布尔标志并使其成为true
    $ b

    • true 表示使用浏览器刷新页面。
    • 按钮。

  • 按钮内部的 onclick 属性,我将 flag 设置为 并调用 window.location.reload()刷新页面。 标签的onload 属性,我检查标志是否真正切换到 code>首页标签其他切换到 Page1 标签其中包含 < div> 标签我想刷新它的内容。之后,将 flag 设置为 true 。*



当涉及到 onload 属性时,'t work和 flag 总是 em> < body> 标记。是否有可能(以某种方式)创建一个变量,在刷新页面时不会改变,在JavaScript中?



* 我的页面包含许多选项卡,第一个是Home(在使用浏览器刷新页面时应显示它),第二个是Page1(它包含 < div> em>标签我想刷新它的内容,并在使用按钮刷新页面时显示它的位置)

解决方案

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



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


  • 将所需的数据存储在cookie中,并在页面加载时读取它

  • 将数据传递到URL并在页面加载时读取当前URL(包括您传递的数据)。例如:将浏览器重定向到 http://something.com/yourpage.html#flag=true 如果用户通过浏览器刷新或重定向到 http://something.com/yourpage。如果用户点击了按钮,那么html#flag = false 。只需阅读当前URL并查看标记是什么即可。 使用cookie创建会话并将数据存储在服务器上(技术上这与第一个选项,除非您在客户端上存储令牌并将其传递给服务器以检索存储在那里的数据)

    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)

    解决方案

    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)

    这篇关于有没有可能创建一个变量,这不会改变刷新页面,在JavaScript中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆