如何用 JavaScript 替换部分 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4664707/
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
how to replace part of the URL with JavaScript?
提问by user63503
I have something like http://domain.com/Pages/SearchResults.aspx?function=search&selectedserver=intranet&search_query=MyRecordsand need to replace it by JavaScript with something similar to http://domain.com/Pages/SearchResults.aspx?function=loginsearch&user=admin&password=admin&selectedserver=intranet&search_query=MyRecords— so
我有类似http://domain.com/Pages/SearchResults.aspx?function=search&selectedserver=intranet&search_query=MyRecords 的东西,需要用类似于http://domain.com/Pages/SearchResults.aspx 的JavaScript 替换它? function=loginsearch&user=admin&password=admin&selectedserver=intranet&search_query=MyRecords— 所以
function=search
is being replaced with
正在被替换
function=loginsearch&user=admin&password=admin
inside the URL. Need help with what JavaScript should I save as a button on a browser's tool bar to click and have the URL in the address bar replaced.
网址内。需要关于我应该将什么 JavaScript 保存为浏览器工具栏上的按钮以单击并替换地址栏中的 URL 的帮助。
回答by Matt Ball
var url = window.location.toString();
window.location = url.replace(/function=search/, 'function=loginsearch&user=admin&password=admin');
回答by tocqueville
The only way to update the displayed URL without reloading the page is history.pushState
在不重新加载页面的情况下更新显示的 URL 的唯一方法是history.pushState
window.history.pushState('', '', '/your-new-url');
回答by Juan Mendes
location.href = location.href.replace(
'function=search&', 'function=loginsearch&user=admin&password=admin&')
回答by Ashley Williams
If you're wanting to do it without refreshing the page, I'm afraid that's impossible. You can only change the hash tag via Javascript, i.e. http://example.com/page/#hashtag, which you can do via window.location.hash
.
如果您想在不刷新页面的情况下执行此操作,恐怕这是不可能的。您只能通过 Javascript 更改哈希标签,即http://example.com/page/#hashtag,您可以通过window.location.hash
.
UPDATE (11th September 2011):See the HTML5 history API DEMOand docs.
回答by Leonid
You may find the dedicated library URI.jshelpful, particularly setQuery()and addQuery()methods. I pasted this chunk of code directly into the console on that page and it seems to work:
您可能会发现专用库URI.js很有帮助,尤其是setQuery()和addQuery()方法。我将这段代码直接粘贴到该页面的控制台中,它似乎有效:
var url = 'http://domain.com/Pages/SearchResults.aspx?function=search&selectedserver=intranet&search_query=MyRecords';
var uri = new URI(url);
var new_params = {'function': 'loginsearch', 'user': 'admin', 'password': 'admin'};
uri.setSearch(new_params);
console.log(uri.toString());
http://domain.com/Pages/SearchResults.aspx?function=loginsearch&selectedserver=intranet&search_query=MyRecords&user=admin&password=admin
<- undefined
>
It should be easy to turn this logic into a function (or a one-liner :)). As an aside, why are you passing the credentials right in the URL?
将此逻辑转换为函数(或单行 :))应该很容易。顺便说一句,为什么要在 URL 中正确传递凭据?
回答by Black
var url = window.location.href;
window.location = url.replace(/function=search/, 'function=loginsearch&user=admin&password=admin');