简体   繁体   English

仅使用javascript重新加载页面一次以加载cookie

[英]Reload page with javascript only one time for load cookies

I have a function that request a data for the user one time. 我有一项功能,可以一次为用户请求数据。 I need reload the page after save these data in a cookie and server read these cookie, but i dont know if these cookie are defined or not. 将这些数据保存在cookie中后,服务器需要读取这些cookie,但是我不知道是否定义了这些cookie,因此需要重新加载页面。 ¿How i reload only one time if i dont have a counter and dont like use parameter? ¿如果我没有计数器并且不喜欢use参数,如何只重新加载一次? the referrer dont change with reload. 引荐来源网址不会随着重新加载而发生变化。

I now have this methot, but i like change for remove parameters: 我现在有了这个方法,但是我喜欢更改删除参数:

function getURLParameter(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null
}

if (getURLParameter('reload') != 'true') {
  //here have function for load cookie
  window.location = window.location.href + '?reload=true';
}

HTTP is a stateless protocol, which mean there is not way - within the protocol - to know the state of a request. HTTP是无状态协议,这意味着在协议内-无法知道请求的状态 For instance : is it the first time it's launched or the second time ? 例如:是第一次启动还是第二次启动? Usual workarounds are adding a parameter to the request, as you suggests or using a cookie on the browser's side. 通常的解决方法是按照您的建议在请求中添加参数,或者在浏览器侧使用Cookie。 This is how sessions are implemented in platforms like Java EE or PHP. 这就是在Java EE或PHP等平台上实现会话的方式。

Why don't you test for another cookie like 'never been reloaded', if it does not exists : create this cookie and reload the page. 如果不存在,为什么不测试另一个cookie,例如“从不重新加载”:创建此cookie并重新加载页面。

The tricky part is when should you delete the cookie , ie : when does your business logic wants you to reload the page again ? 棘手的部分是您何时应删除cookie ,即:您的业务逻辑何时希望您重新加载页面? That's up to you to decide. 由您决定。

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

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