-3

I'm passing few parameters with GET Method to a domain example :

domain.com?bid=23&color=red&gender=male

I can't use PHP and I need to grab those parameters value (bid,color,geneder)and to transfer them to new domain with redirection , this is how I did it with PHP

header( "Location: http://seconddomain.com/page3.php?c=50&key=f342fd0eb8ee0b6d9740f85971dabfec&bid=".$_GET["bid"]."&color=".$_GET["color"]."&gender=".$_GET["gender"]." );

http://seconddomain.com/page3.php?c=50&key=f342fd0eb8ee0b6d9740f85971dabfec this is just the page..

How can I convert it to javascript this is possible?

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
  • Convert what to javascript? You could just do the redirect in javascript directly, as you already showed. – developerwjk Sep 25 '14 at 21:17
  • Please read [window.location](https://developer.mozilla.org/en-US/docs/Web/API/Window.location) – Jay Blanchard Sep 25 '14 at 21:18
  • 1
    possible duplicate of [How do I get the value of a URL paramater in javascript?](http://stackoverflow.com/questions/7243520/how-do-i-get-the-value-of-a-url-paramater-in-javascript) or http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript –  Sep 25 '14 at 21:18

3 Answers3

0

Parse the getUrls var then redirect to the wanted page with the wanted parameters

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.search.substring(1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
var urlGetPrams = getUrlVars();
var redirecTo = "http://seconddomain.com/page3.php?c=50&key=f342fd0eb8ee0b6d9740f85971dabfec&bid="+urlGetPrams["bid"]+"&color="+urlGetPrams["color"]+"&gender="+urlGetPrams["gender"];
 // Redirect to 'redirectTo' url
window.location = redirecTo;
Isaac
  • 983
  • 1
  • 7
  • 13
0

You can't make a GET (neither POST) request with javascript to another domain because of the same origin policy. You may find a solution here : Ways to circumvent the same-origin policy

Community
  • 1
  • 1
KVM
  • 878
  • 3
  • 13
  • 22
0

You could parse document.location.search to retrieve the query string parameters.

i.e:

function getParameters(){
    var parameters = {}, regex = "[\?|\&]([^=]+)\=([^&]+)", greedy= new RegExp( regex, "g"), single = new RegExp( regex ), parts;

    document.location.search.replace( greedy,function( group ) {
        parts = single.exec( group );
        parameters[ parts[ 1 ] ] = parts[ 2 ];
    });

    return parameters;
}

if you call on your url: 'http://seconddomain.com/page3.php?c=50&key=f342fd0eb8ee0b6d9740f85971dabfec' it will return a map like

{ c : 50, key : f342fd0eb8ee0b6d9740f85971dabfec }

Bruno Queiroz
  • 367
  • 2
  • 9