最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Is it possible to create a variable, that doesn't change on refresh the page, in javascript? - Stack Overflow

programmeradmin2浏览0评论

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 es 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)

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 es 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)

Share Improve this question edited May 23, 2017 at 11:55 CommunityBot 11 silver badge asked Aug 28, 2011 at 2:39 Eng.FouadEng.Fouad 118k74 gold badges324 silver badges427 bronze badges 1
  • 1 this sounds like a perfect cookie situation. – Joe Commented Aug 28, 2011 at 2:43
Add a ment  | 

4 Answers 4

Reset to default 3

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./yourpage.html#flag=true if the user refreshed through the browser or redirect to http://something./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)

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.

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...

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论