Skip to content Skip to sidebar Skip to footer

How To Extract Relative Url From Argument Values From Request String?

I have a request url / string var like http://ip_or_url:4773/image_renderer.service?action=resize&from_format=png&from_url=http://ip_or_url:4773/my_file.user.file&to_fo

Solution 1:

With pure javascript. It'll return an array of key/value pairs.

functiongetUrlParts(url){
    // url contains your data.var qs = url.indexOf("?");
    if(qs==-1) return [];
    var fr = url.indexOf("#");
    var q="";
    q  = (fr==-1)? url.substr(qs+1) : url.substr(qs+1, fr-qs-1);
    var parts=q.split("&");
    var vars={};
    for(var i=0;i<parts.length; i++){
        var p = parts[i].split("=");
        vars[unescape(p[0])]= p[1] ? unescape(p[1]):"";
    }
    // vars contain all the variables in an array.return vars;
}

Let me know if any of your test cases fails.

Solution 2:

When the url is in the varaible sUrl:

var sFromUrl = sUrl.match(/from_url=[^&]*/)[0];
var sBasename = sFromUrl.match(/[^\/]*$/)[0];

Also see this example.

Solution 3:

Use this to get your url variables in JS

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    returnvars;
}

and then string.substring(string.search("/")) to get your piece of string

Post a Comment for "How To Extract Relative Url From Argument Values From Request String?"