22

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

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.

2
  • that's an error, already fixed Commented Jan 12, 2011 at 2:02
  • 31
    Please tell me you are not sending a username and password in cleartext. Commented Jan 12, 2011 at 2:02

6 Answers 6

22
var url = window.location.toString();
window.location = url.replace(/function=search/, 'function=loginsearch&user=admin&password=admin');
Sign up to request clarification or add additional context in comments.

6 Comments

added a button with the following code: javascript:var url = window.location.toString();window.location = url.replace(/function=search/, 'function=loginsearch&user=admin&password=admin'); and it worked! Thanks a lot!
@Juan: depends on if it's in an onclick attribute or not.
There's no reason to run it as href="javascript: {...}"
Changing something in the URL is just possible by JS? Actually I'm trying to replace all spaces with + in the URL. How can I do that?
@MattBall What why? Why I what to replace spaces with + or why I want to do that by something else (not JS)?
|
11

The only way to update the displayed URL without reloading the page is history.pushState

window.history.pushState('', '', '/your-new-url');

Comments

5
 location.href = location.href.replace(
    'function=search&', 'function=loginsearch&user=admin&password=admin&')

Comments

1

My version. Works fine for me.

let url = window.location.toString();
window.history.pushState('', '', url.replace(searching_string, new_string));

Comments

0

You may find the dedicated library URI.js helpful, particularly setQuery() and addQuery() methods. 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?

Comments

0
var url             = window.location.href;               
window.location     = url.replace(/function=search/, 'function=loginsearch&user=admin&password=admin');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.