简体   繁体   中英

Javascript go back with query added to url

on my logout page I redirect users back to the last page they was on, but I'm currently trying to add &msg=2 onto the URL, but I can't work out how to do it. I've tried doing

<script>
  history.back(-1) + "&msg=2";
</script>

But surprise surprise, it doesn't work. So any ideas how I would do this? Sorry for such an easy question, I'm kinda new to JS.

尝试这个

window.location.replace(document.referrer+ "&msg=2");

This seems a little fragile. It will fail (or at least do something unexpected) if, for example, another site links to your logout URL. You also don't have access to the history URLs, you can only tell the browser to go forwards or backwards.

I would approach this by adding a URL parameter to your logout script that contains the URL of where the browser should go afterwards (the continue URL). The server serves a 302 to the continue URL with the added parameter, or defaults to your home page.

Pseudo code:

handleLogout(httpRequest){
  doLogout();
  queryParams = httpRequest.getQueryParameters();
  if (queryParams.get('continue')):
    continueUrl = queryParams.get('continue');
    redirectUrl = continueUrl.appendQueryParam('msg', '2');
    return Http302Response(redirectUrl);
  else:
    return Http302Response('my_home_page');

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