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

查看:19
本文介绍了隐藏在 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.

我试图避免问题是 X 的 XY 问题,我询问了 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天全站免登陆