隐藏在URL中传递的变量 [英] hide variables passed in URL

查看:138
本文介绍了隐藏在URL中传递的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在研究一个Web应用程序,并且我们刚刚完成了它,但有一件事情让我们感到困扰(尽管决不会停止生产)。

We've been working on a web application and we've just about got it finished up, but there's one thing that bothering us (although by no means is it going to stop production.)

当我们调用其中一个页面(index.html)时,我们有时必须在URL中传递一个变量(searchid)。所以我们得到一个像 http://domain.com/index.html?searchid=string 的页面。

When we call one of the pages (index.html), we sometimes have to pass it a variable in the URL (searchid). So we get a page like http://domain.com/index.html?searchid=string.

我们最好不要显示?searchid = string ,但我不确定我们会怎么做。

We'd ideally like to not show the ?searchid=string, but I'm not sure how we'd do that.

我的小组没有拥有index.html页面(但是我们正在处理那个小组),所以我不知道我们能够处理多少东西,比如 .htaccess 或类似的。

My group doesn't own the index.html page (but we are working with the group that does), so I don't know how much we'd be able to do with anything like .htaccess or similar.

我在考虑 POST ,但我不知道如何只用HTML和jQuery来接收它。我的小组中的另一个人认为,在加载页面后,我们可以从URL中删除它,但我认为我们需要刷新页面,然后丢失数据。

I was thinking about POSTing the variable, but I don't know how to receive it with just HTML and jQuery. Another person in my group thought that after the page loaded we could remove it from the URL, but I assume we would need a page refresh which would then lose the data anyway.

我试图避免XY问题,其中问题是X并且询问Y,那么从URL中删除变量的正确方法是什么?

I'm trying to avoid XY problem where the problem is X and I ask about Y, so what's the right way to remove the variable from the URL?

推荐答案

您的问题似乎指示目标网页不是,不会由某个服务器端脚本驱动。如果是这样的话,我建议将查询字符串改为哈希,它具有直接可编辑的优点,而不会触发页面载入:

Your question seems to indicate that the target page is not and will not be powered by some server-side script. If that's the case, I'd suggest changing the querystring to a hash, which has the advantage of being directly editable without triggering a page-load:

http://yourdomain.com/page.html#search=value

<script type='text/javascript'>
  // grab the raw "querystring"
  var query = document.location.hash.substring(1);

  // immediately change the hash
  document.location.hash = '';

  // parse it in some reasonable manner ... 
  var params = {};
  var parts = query.split(/&/);
  for (var i in parts) {
    var t = part[i].split(/=/);
    params[decodeURIComponent(t[0])] = decodeURIComponent(t[1]);
  }

  // and do whatever you need to with the parsed params
  doSearch(params.search);
</script>

尽管如此,为了获得某些服务器端脚本,它会更好这里。

Though, it would be better to get some server-side scripting involved here.

这篇关于隐藏在URL中传递的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆