[英]how to replace part of the URL with JavaScript?
I have something like http://domain.com/Pages/SearchResults.aspx?function=search&selectedserver=intranet&search_query=MyRecords and 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。
var url = window.location.toString();
window.location = url.replace(/function=search/, 'function=loginsearch&user=admin&password=admin');
在不重新加载页面的情况下更新显示的 URL 的唯一方法是history.pushState
window.history.pushState('', '', '/your-new-url');
location.href = location.href.replace(
'function=search&', 'function=loginsearch&user=admin&password=admin&')
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, ie 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 DEMO and docs .更新(2011 年 9 月 11 日):请参阅 HTML5 历史 API演示和文档。
My version.我的版本。 Works fine for me.
对我来说很好。
let url = window.location.toString();
window.history.pushState('', '', url.replace(searching_string, new_string));
You may find the dedicated library URI.js helpful, particularly setQuery() and addQuery() methods.您可能会发现专用库URI.js很有帮助,尤其是setQuery()和addQuery()方法。 I pasted this chunk of code directly into the console on that page and it seems to work:
我将这段代码直接粘贴到该页面上的控制台中,它似乎可以工作:
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 中直接传递凭据?
var url = window.location.href;
window.location = url.replace(/function=search/, 'function=loginsearch&user=admin&password=admin');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.