Javascript: Using variables from current URL to create and open new URL

Go To StackoverFlow.com

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

2012-04-04 21:31
by JohnAno
What have you tried? What code do you have so far? What purpose are you trying to achieve - Josh 2012-04-04 21:33
Take a look at this

http://stackoverflow.com/questions/901115/get-query-string-values-in-javascrip - Filype 2012-04-04 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 2012-04-04 21:41
I can't understand the changes. What's the &example in the middle? Where do xxxx and yyyy go - Whymarrh 2012-04-04 21:42
@Filype Those solutions mentioned referring to that question all seem pretty complicated.. - JohnAno 2012-04-04 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 2012-04-04 21:45
Getting the variables in the URL is complicated. There's no built-in way in JavaScript to do that - Simon Forsberg 2012-04-04 21:48
@JohnAno see the answers below - Whymarrh 2012-04-04 22:41


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);
2012-04-04 22:04
by jfriend00


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.

2012-04-04 22:10
by Whymarrh
Ads