How to textract & client = from url?

If my url http://link-ads.blogspot.com/?url=http://could-be-any-url.com&name=123456789101112uses pure javascript , how would I extract &client=so that it extracts 123456789101112and sets it asvar clientid = 123456789101112;

+5
source share
4 answers
var clientid = (window.location.search.match(/[?&;]name=(\d+)/) || [])[1];

If it can contain more numbers, use something like ...

var clientid = (window.location.search.match(/[?&;]name=([^&;]+)/) || [])[1];

If the parameter is not found, it will return undefined.

If you need to do this more often, you will be better off with a common solution .

0
source

try the following:

var url = "http://link-ads.blogspot.com/?url=http://could-be-any-url.com&name=123456789101112"

var name = url.substring(url.indexOf('&name='), url.length)

If you do not want to use user regex, like another answer

0
source

/ - :

getParameter = function(paramName, url) {
    if (! paramName) {
        return null;
    }
    if (! url) {
        url = window.location.href;
    }
    if (url.indexOf('?') == -1) {
        return null;
    }
    url = url.substring(url.indexOf('?') + 1);
    var params = url.split('&');
    for (var index = 0; index < params.length; index++) {
        var nameValue = params[index].split('=');
        if (nameValue.length == 2 && nameValue[0] == paramName) {
            return nameValue[1];
        }
    }

    return null;
};

... getParameter('client');.

: http://jsfiddle.net/L7hbT/3/

0
source

Not as concise as @alex, but you can do it

var a = 'http://link-ads.blogspot.com/?url=http://could-be-any-url.com&name=123456789101112';

var b = a.indexOf('name=');

var clientId = a.substring(b + 5);

alert(clientId);

Example: http://jsfiddle.net/jasongennaro/ubJe3/

0
source

All Articles