1

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
take2
  • 616
  • 2
  • 17
  • 31
  • Can you put your code you are using to generate this URL. Without the complete code, we couldn't see how to help – User 99x Oct 22 '12 at 12:17
  • That is the problem, the script is huge, I can't paste it here and have no idea how the URL is generated. That's why I said that I need a solution that would just take the URL and change this parameter, if it's possible of course. – take2 Oct 22 '12 at 12:19
  • Yes its possible.. could you access the variable that holds the parameter? if so, then you could use regular expression to replace the parameter – User 99x Oct 22 '12 at 12:22
  • What do you mean by "access the variable that holds the parameter"? I know what will be outputed there (always the same) and know what I want to be there (always the same). So I don't need any text to load dinamically as a parameter, if that is what you mean. – take2 Oct 22 '12 at 12:29

4 Answers4

1

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;
}
Community
  • 1
  • 1
Stefan
  • 3,850
  • 2
  • 26
  • 39
  • This would be if the URL is always the same or? It changes dinamically, but this parameter is always fixed. – take2 Oct 22 '12 at 12:24
  • @take2: You are correct. Add a loop over lst_parts and create an associative array with query parameters and values, and then use this solution: http://stackoverflow.com/a/111545/888177 – Stefan Oct 22 '12 at 12:29
  • 1
    That is close, but on line 7 x is undefined. – Asad Saeeduddin Oct 22 '12 at 12:33
  • @Asad: Yep. Unfortuately won't work if x is not always in the same position. – Stefan Oct 22 '12 at 12:34
  • That would be a solution to dinamically load the URL? Because in that code I don't see where should I add the new parameter (change "preparameter" to "postparameter"). – take2 Oct 22 '12 at 12:34
  • I would definitely prefer your solution, if it can be combined with dinamical URLs, because it is clear which parameter value I'm changing with what. – take2 Oct 22 '12 at 12:35
  • In that code you can just change the value in the array, like this: data['ThisParameter'] = 'BlaBla' – Stefan Oct 22 '12 at 12:36
  • Here is what I did: 1. Added this code (joined two of these) in a script loaded on the page where the search button is. 2. Changed 'data['ThisParameter'] = new_val;' to 'data['ThisParameter'] = something;'. It still loads the same parameter value, but I suppose I should do something else too. Should I somehow point where does this parameter come (first, second...)? – take2 Oct 22 '12 at 12:47
  • Did you do this too? : `new_url = lst_tmp[0] + '?' + EncodeQueryData(data); window.location.href = new_url;` – Stefan Oct 22 '12 at 12:50
  • It contained some errors, might be ok now. Please check the errors and warnings in your javascript console and make sure `new_url` is the url you require (use a statement `alert(new_url)` before you call `window.location.href` – Stefan Oct 22 '12 at 12:56
  • Now there is the exact URL specified again, is that intentional? Because the URL where the submit button is doesn't have any parameters, parameters are in the URL that is being opened when you click search button. – take2 Oct 22 '12 at 13:03
  • OK, change the exact url to `window.location` or `window.location.href` as I now show at the beginning of my code. – Stefan Oct 22 '12 at 13:04
  • It still doesn't work. I will just try one more thing, won't bother you more again - how do you specify "var key = "ThisParameter";" and "var new_val = 'blabla';". If URL is "...&ThisParameter=ABC..."... I have tried a few combinations, e. g. like this "var key = "ThisParameter";" and "var new_val = 'DEF';" – take2 Oct 22 '12 at 13:11
  • No problem. Only use the second part of my answer (the part after the EDIT) just as it is. Or just use charlietfl's answer. – Stefan Oct 22 '12 at 13:13
  • Thanks for the effort too, there are no errors in the console, but it doesn't change the parameter anyway. I will try on the other pages with some search functionality, to see if it works there. There is probably something conflicting with this first script. – take2 Oct 22 '12 at 13:22
  • @Stefan After hours of trying, I have managed to make your code work partly on a simple search system. The problem was thath "lst_tmp" wasn't defined, so I have changed it to "lst" and it changed the parameter. However, it deletes the part of the URL after the domain. Instead of "www.example.com/alias&parameter=ABC" I get "www.example.com/ABC&parameter=ABC". However, I have found the part of the code in the script that impacts this parameter, so please check my edit of the quesiton, it should be much easier to impact the URL now. – take2 Oct 23 '12 at 15:20
  • @take2 OK, I found a typo in my code and fixed it. This should be: `var new_url = lst_temp[0] + '?' + EncodeQueryData(arr_data);` But, in your code, can't you just do `options['ThisParameter'] = 'Something';` Just add this statement after `var options = {'Foo...};`. This will change the parameter for you without needing my code. – Stefan Oct 23 '12 at 17:24
  • By the way, a correct url should be something like `http://www.example.com/index.php?parameter=ABC&param2=XYZ` and not `http://www.example.com/index.php&parameter=ABC&param2=XYZ`. Thus a ? instead of a & for the first query parameter. – Stefan Oct 23 '12 at 18:06
  • @Stefan I don't have the access to the script, it just gets loaded dynamically. When it gets loaded, I need to "overwrite" that parameter. – take2 Oct 23 '12 at 18:08
  • @take2 OK sorry yes I realize that now. Just try my code now, it should work fine. – Stefan Oct 23 '12 at 18:10
  • @Stefan It doesn't work with that script I mentioned in the first post, but works elsewhere. Therefore, I will mark this as fixed and open a new question about how to override that option mentioned in the edit. P. S. Nice code, thank you, hopefully you'll know the answer to the next question too :) I'll post the link here – take2 Oct 23 '12 at 18:14
  • @Stefan http://stackoverflow.com/questions/13036770/how-to-override-variable-parameter-loaded-from-another-script – take2 Oct 23 '12 at 18:23
1

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('&');
}
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • I have tried this, but now I get endless refreshing of the page. I have put the script on thep age where the submit button is (it has to be like that). URL of the page where the submit button is is "normal", no parameters or anything similar. – take2 Oct 22 '12 at 12:49
  • ahh yes, endless refresh ..makes sense. You need a way to locate and modify the url before the page gets loaded if you do it with script. Not enough detail given to do that. Alternative is to do it at server – charlietfl Oct 22 '12 at 12:55
  • You can avoid endless refresh by only setting location if the new value is not equal to the old value. – Stefan Oct 22 '12 at 12:59
  • 1
    I made adjustment to code with condition to only reload if value not already set – charlietfl Oct 22 '12 at 13:02
  • Thanks for the effort, the code seems good, no errors in the console, but it doesn't change the parameter in this case. I will try on the other pages with some search functionality, to see if it works there. There is probably something conflicting with this first script. – take2 Oct 22 '12 at 13:21
  • try logging the revised value to console and see what happens. Is hard to test here. Also, is possible that server is storing the values perhaps in session or something. A server side solution would be less of a hack – charlietfl Oct 22 '12 at 13:22
  • @charlietfl I have spent hours testing these codes and your code still produces endless refresh on a simple search system. New URL opens (it isn't changed) and it refreshes endlessly..I have also learned how to use Firebug DOM better and found one part of the code that impacts this parameter, please check my edit of the question. – take2 Oct 23 '12 at 15:18
  • best to just log the values to browser console and double check them rather than do the redirect – charlietfl Oct 23 '12 at 21:08
0

You could simply regex replace, or split by ? then by & to get an array of the parameter value pairs.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
0

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);

lpratlong
  • 1,421
  • 9
  • 17