Javascript 隐藏在 URL 中传递的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13650408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hide variables passed in URL
提问by Rob
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.)
我们一直在开发一个 Web 应用程序,我们刚刚完成了它,但是有一件事情困扰着我们(尽管它绝不会停止生产。)
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.
当我们调用其中一个页面 (index.html) 时,我们有时必须在 URL 中传递一个变量 (searchid)。所以我们得到一个像http://domain.com/index.html?searchid=string.
We'd ideally like to not show the ?searchid=string, but I'm not sure how we'd do that.
理想情况下,我们不希望显示?searchid=string,但我不确定我们将如何做到这一点。
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 .htaccessor similar.
我的小组不拥有 index.html 页面(但我们正在与拥有该页面的小组合作),所以我不知道我们能够对类似.htaccess或类似的内容做多少。
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.
我正在考虑POSTing 变量,但我不知道如何仅使用 HTML 和 jQuery 接收它。我组中的另一个人认为在页面加载后我们可以从 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?
我试图避免问题是 X 的 XY 问题,我询问 Y,那么从 URL 中删除变量的正确方法是什么?
采纳答案by svidgen
Your question seemsto indicate that the target page is not and will notbe 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 directlyeditable without triggering a page-load:
您的问题似乎表明目标页面不是也不会由某些服务器端脚本提供支持。如果是这种情况,我建议将查询字符串更改为散列,其优点是可以直接编辑而不触发页面加载:
http://yourdomain.com/page.html#search=value
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 betterto get some server-side scripting involved here.
不过,最好在此处涉及一些服务器端脚本。
回答by lostsource
You can use the History API, but it does require a modern browser
您可以使用 History API,但它确实需要现代浏览器
history.replaceState({}, null, "/index.html");
That will cause your URL to appear as /index.html without reloading the page
这将导致您的 URL 显示为 /index.html 而无需重新加载页面
More information here:
更多信息在这里:
回答by zzzzBov
It's possible to rewrite the URL using JavaScript's history API. History.jsis a library that does this very well.
可以使用 JavaScript 的历史 API 重写 URL。History.js是一个可以很好地做到这一点的库。
That being said, I don't think there's any need for removing the query-string from the URL, unless you're dynamically changing the contents of the page to make the current query-string irrelevant.
话虽如此,我认为没有必要从 URL 中删除查询字符串,除非您动态更改页面内容以使当前查询字符串无关紧要。
回答by John Dvorak
You could post the data, then let the server include the posted data in the page, e.g.:
您可以发布数据,然后让服务器在页面中包含发布的数据,例如:
echo "<script> post_data = ".json_encode($_POST)." </script>";
This works cross-browser.
这可以跨浏览器工作。

