简体   繁体   English

Javascript返回查询添加到url

[英]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. 在我的注销页面上,我将用户重定向回到他们所在的最后一页,但我目前正在尝试将&msg=2添加到URL上,但我无法弄清楚如何操作。 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. 对不起,这个简单的问题,我是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. 例如,如果另一个站点链接到您的注销URL,它将失败(或至少做一些意外的事情)。 You also don't have access to the history URLs, you can only tell the browser to go forwards or backwards. 您也无权访问历史记录URL,只能告诉浏览器前进或后退。

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). 我会通过在您的注销脚本中添加一个URL参数来实现此目的,该脚本参数包含浏览器之后应该到达的URL( 继续 URL)。 The server serves a 302 to the continue URL with the added parameter, or defaults to your home page. 服务器使用添加的参数为继续URL提供302,或者默认为您的主页。

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');

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

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