2

I would like to redirect a user to a target URL on a button click. The target URL is variable and has to be read from the current page URL parameter 'source':

For instance, I have a url http://w/_l/R/C.aspx?source=http://www.google.com

When the user clicks on a button he's being redirect to http://www.google.com

How would I do that with jQuery?

jjmontes
  • 24,679
  • 4
  • 39
  • 51
user472285
  • 2,604
  • 5
  • 37
  • 55

7 Answers7

3

first of all you need to get the url param : source this can be done with a function like :

function GetParam(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
} 

// you can use it like 
var source = GetParam('source');
//then
window.location.href = source
amd
  • 20,637
  • 6
  • 49
  • 67
0

On button click handler, just write window.location.href = http://www.google.com

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
  • -1 as it is obvious that the OP needs to redirect to the URL defined in the URL parameter 'source'. – jjmontes Jan 05 '12 at 10:41
0

get url params : (copied from another stackoverflow question) :

var params= {};

document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
    function decode(s) {
        return decodeURIComponent(s.split("+").join(" "));
    }

    params[decode(arguments[1])] = decode(arguments[2]);
});

window.location = params['source'];
redmoon7777
  • 4,498
  • 1
  • 24
  • 26
  • $_GET is not a traditionally accepted name for a Javascript variable. That's how request parameter map is called in PHP, but it may be a bit misleading here. – jjmontes Jan 05 '12 at 10:38
  • 1
    I just did not want to alter the source where I got it ()I will change it now – redmoon7777 Jan 05 '12 at 17:47
0

You will need to parse the query string to get the value of the variable source. You don't need jQuery for it.

A simple function like this will suffice:

function getFromQueryString(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

location.href = getFromQueryString("source");
nunespascal
  • 17,584
  • 2
  • 43
  • 46
  • No, it does not. It is only splitting the query string, checking for the GET parameter you are looking for("source" in this example), and returning the appropriate value. – nunespascal Jan 11 '12 at 05:07
0

Using the url parsing code from here use this to parse your url (this should be included once in your document):

var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();

Then do this to redirect to the source parameter:

window.location.href = urlParams["source"];
Community
  • 1
  • 1
davidethell
  • 11,708
  • 6
  • 43
  • 63
0

Since you are using the jQuery framework, I'd make use of the jQuery URL Parser plugin, which safely parses and decodes URL parameters, fragment...

You can use it like this:

var source = $.url().param('source');
window.location.href = source;
jjmontes
  • 24,679
  • 4
  • 39
  • 51
-1

You can do like this,

<a id="linkId" href=" http://w/_l/R/C.aspx?source=http://www.google.com">Click me</a>


$('#linkId').click(function(e){
  var href=$(this).attr('href');
  var url=href.substr(href.indexof('?'))
  window.location =url;
  return false;   

});
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92