-1

I have got a current URL looking like this:

http://example?variables1=xxxx&example&variables2=yyyyy

I want to use the variables1 and variables2 to create a new URL and open this new URL:

http://example?variables3=variables1&example&variables4=variables2

I hope someone can help me with this :)

filype
  • 8,034
  • 10
  • 40
  • 66
JohnAno
  • 9
  • 1
  • What have you tried? What code do you have so far? What purpose are you trying to achieve? – Josh Apr 04 '12 at 21:33
  • Take a look at this http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript – filype Apr 04 '12 at 21:34
  • I want to bypass a button click, because i cant manage to make the Script click that button. This button in fact creates such a URL and opens it. – JohnAno Apr 04 '12 at 21:41
  • I can't understand the changes. What's the `&example` in the middle? Where do `xxxx` and `yyyy` go? – Whymarrh Apr 04 '12 at 21:42
  • @Filype Those solutions mentioned referring to that question all seem pretty complicated... – JohnAno Apr 04 '12 at 21:43
  • @Whymarrh: I could also have wrote: variables3=xxxx and variables4=yyyy , the example? in the middle should just show that there is a piece of text in the URL between the two variables. – JohnAno Apr 04 '12 at 21:45
  • Getting the variables in the URL is complicated. There's no built-in way in JavaScript to do that. – Simon Forsberg Apr 04 '12 at 21:48
  • @JohnAno see the answers below. – Whymarrh Apr 04 '12 at 22:41

2 Answers2

0

You will need to parse the desired query parameters from the first URL and use string addition to create the second URL.

You can fetch a specific query parameter from the URL using this code. If you were using that, you could get variables1 and variables2 like this:

var variables1 = getParameterByName("variables1");
var variables2 = getParameterByName("variables2");

You could then use those to construct your new URL.

newURL = "http://example.com/?variables1=" + 
    encodeURIComponent(variables1) + 
    "&someOtherStuff=foo&variables2=" + 
    encodeURIComponent(variables2);
Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Because I don't fully understand what needs to change, here's my best attempt*, using a mashup of other answers and resources online.

// the original url
// will most likely be window.location.href
var original = "http://example?variables1=xxxx&example&variables2=yyyyy";

// the function to pull vals from the URL
var getParameterByName = function(name, uri) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(uri);

    if(results == null) return "";
    else return decodeURIComponent(results[1].replace(/\+/g, " "));
};

// so, to get the vals from the URL
var variables1 = getParameterByName('variables1', original); // xxxxx
var variables2 = getParameterByName('variables2', original); // yyyyy

// then to construct the new URL
var newURL =  "http://" + window.location.host;
    newURL += "?" + "variables3=" + variables1;
    newURL += "&example&"; // I don't know what this is ...
    newURL += "variables4=" + variables2;

// the value should be something along the lines of
// http://example?variables3=xxxx&example&variables4=yyyy

*All of which is untested.

Community
  • 1
  • 1
Whymarrh
  • 13,139
  • 14
  • 57
  • 108