简体   繁体   中英

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. ¿How i reload only one time if i dont have a counter and dont like use parameter? 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. 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. This is how sessions are implemented in platforms like Java EE or 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.

The tricky part is when should you delete the cookie , ie : when does your business logic wants you to reload the page again ? That's up to you to decide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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