Reputation: 614
I apologize in advance for not being able to supply more information, if this is not possible with the limited info, I will delete the question.
So, I have a script that generates kind of a search engine that depends on the script (also dinamically generated) which is quite complicated and I don't have any impact on it.
When you click on the apply (search) button, the search process is triggered and you can see the results on the new URL that has a few parameters.
E. g.: http://www..example.com/...&ThisParameter=XYZ&SecondParameter=foo&ThirdParameter=bar
My question is - is it possible to change "ThisParameter=XYZ"
to something else, e. g. "ThisParameter=Something"
.
The problem is that I don't know how the URL is constructed and can'i impact it, so I would need a code that would just take any URL generated and change this part mentioned.
Thank you for your opinions in advance!
EDIT
I have found the part of the script that impacts the URL generated. When I change it in DOM, it does redirect me to the desired page.
var options = {"FOO":123,"BAR":"ABC","ThisParameter":"STH","EXAMPLE":123, ...}
And this is the output when you choose "copy path":
_options[0].ThisParameter
Upvotes: 1
Views: 4662
Reputation: 3900
IF THE SEQUENCE OF QUERY PARAMETERS ARE ALWAYS THE SAME:
var url = window.location.href;
var lst_temp = url.split('?');
var $lst_parts = lst_temp[1].split('&');
var new_val = 'blabla';
var position = 'x';
lst_parts[x] = 'ThisParameter=' + new_val;
var url_new = lst_temp[0] + '?';
url_new += lst_parts.join('&');
EDIT:
IF ORDERING OF QUERY KEYS ARE NOT KNOWN BEFORE HAND, DO THIS:
Use this function from here: https://stackoverflow.com/a/111545/888177
function EncodeQueryData(arr_data) {
var ret = [];
for (var d in arr_data)
ret.push(encodeURIComponent(d) + "=" + encodeURIComponent(arr_data[d]));
return ret.join("&");
}
Then read the url and split it like this:
var url = window.location.href;
var key = "ThisParameter";
var new_val = 'blabla';
var lst_temp = url.split('?');
var lst_parts = lst_temp[1].split('&');
Add this loop to create an associative array containing the query parameters and values:
var arr_data = new Array();
for (var index in lst_parts) {
lst = lst_parts[index].split('=');
arr_data[lst[0]] = lst[1];
}
To avoid endless refresh:
var val = arr_data[key];
if (val != new_val) {
arr_data[key] = new_val;
var new_url = lst_temp[0] + '?' + EncodeQueryData(arr_data);
window.location.href = new_url;
}
Upvotes: 1
Reputation: 171669
You won't be able to change the url without reloading the page but the following will allow you to make the adjustment. It takes the url of current page ( source is unclear in explanation). If the url you refer to is in an href of a link this code can be simply modified
/* get current url info using location object*/
var baseUrl = location.href.split('?')[0];
/* array of search strings */
var searchParams = location.search.slice(1).split('&');
var searchKeyToChange = 'ThisParameter';
var newVal = 'someNewValue';
var doPageReload = false;
/* loop over search strings, modify appropriate one*/
for (i = 0; i < searchParams.length; i++) {
var params = searchParams[i].split('=');
if (params[0] == searchKeyToChange) {
if (params[1] != newVal) {
searchParams[i] == searchKeyToChange + '=' + newVal;
doPageReload = true;
}
}
} /* load adjusted url if new value not already set*/
if (doPageReload) {
location = baseUrl + '?' + searchParams.join('&');
}
Upvotes: 1
Reputation: 1461
If your question is how about to change dynamically an URL in your browser with JavaScript, it is not possible (for security reasons). If you just need to request a page by using then changing your URL, you can use the ways from previous comment then : window.location.replace(yourModifiedUrl);
Upvotes: 0
Reputation:
You could simply regex replace, or split by ?
then by &
to get an array of the parameter value pairs.
Upvotes: 0