0

I have a url string like this: example.com/sfm?dir=uploads/sfm/root/folder1/subfolder1

Is it possible to catch only the part after the = with javascript? So the catched part should be this: uploads/sfm/root/folder1/subfolder1

6 Answers 6

2

You may try this (Taken From MDN):

var getQueryVarByName = function (name)
{
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};

Use:

var param = getQueryVarByName('dir'); // uploads/sfm/root/folder1/subfolder1
Sign up to request clarification or add additional context in comments.

Comments

1

You can use split to split string at = and get second part

var str = 'example.com/sfm?dir=uploads/sfm/root/folder1/subfolder1';
var part = str.split('=')[1];
console.log(part);

Comments

0

You can use .split() to get it:

Working Example

var url = "example.com/sfm?dir=uploads/sfm/root/folder1/subfolder1";

var s = url.split('=')[1];

Comments

0

You can try this (Using the .split()):

function getFilePath(fullPath){
    return fullPath.split("dir=")[1];
}

Now call it the following way:

getFilePath("example.com/sfm?dir=uploads/sfm/root/folder1/subfolder1")

Comments

0

You need to create regexp. If there are more parameters after dir regular expression should be more complicated.

var url = 'example.com/sfm?dir=uploads/sfm/root/folder1/subfolder1';
var regexp = /dir=([\w\W]*)/;
var matched = url.match(regexp);

document.body.innerHTML = matched[1];

4 Comments

oke this seems to be no problem. But now i also need to read the url string with javascript first. How can i do that?
use window.location.href
oke i alreadyhave that. Now one more question: i want bind th result to a php variable. How can i do that?
You can't. JavaScript runs in browser (at least in this case) and PHP on server, so you need to send query to server. But that is outside of scope of this question.
0

The part after question mark ? is known as window.location.search. It is possible that you have than 1 parameter in this querystring. So safe solution should be like this:

//example.com/sfm?dir=uploads/sfm/root/folder1/subfolder1&un=test&time=1234
function getPath(){
var query=window.location.search //?dir=uploads/sfm/root/folder1/subfolder1&un=test&time=1234
   .replace(/^\?/,'') //dir=uploads/sfm/root/folder1/subfolder1&un=test&time=1234
   .split('&'); //[dir=uploads/sfm/root/folder1/subfolder1, un=test, time=1234]
for(var i = 0, n = query.length;i < n;i++){
   var qs = query[i].split('=');
   if(qs.length == 2 && qs[0] == 'dir')
      return qs[1]; //if found
}//for
return null; // not found
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.